recently came across an interesting feature to C++11: std::tie. One of the features I love about python is that you are able to return multiple values from a function. These get returned in a structure called a “Tuple”. Here is a basic example of what I mean:

class rectangle:def __init__(self, width, height):self.width = widthself.height = heightdef get_dimensions(self):return self.width, self.heightr = rectangle(3,4)w, h = r.get_dimensions()print w,h

When run this will print “3 4”. The classic way to return multiple parameters in C/C++ is to pass extra parameters by reference or as pointers. The return values then got written to these parameters. While this worked it wasn’t the clearest thing to read since there was no clear division between parameters and return values. Infact, sometimes they served the same purpose of providing data and returning with a value. Here is an example of the same function in the old C++ way:

#include <iostream>using std::cout;class rectangle{public:rectangle(int _width, int _height) : width(_width), height(_height) {}int width, height;void get_dimensions(int & _width, int & _height){_width = width;_height = height;}};int main(int argc, char** argv){rectangle r(3,4);int w,h;r.get_dimensions(w,h);cout << w << ' ' << h << '\n';return 0;}

As you can see  w  and  h  are passed in like normal parameters with no indication to the user their real purpose.

Luckily C++11 has introduced tuple types and a way to return multiple values from a function. To use this all you need to do is return a tuple from a function, and then use std::tie to accept the return value. std::tie create its own tuple storing references to the variables it needs to throw the data into. Heres the same example:

#include <tuple>#include <iostream>using std::tuple;using std::tie;using std::make_tuple;using std::cout;class rectangle{public:rectangle(int _width, int _height) : width(_width), height(_height) {}int width, height;tuple<int, int> get_dimensions() {return make_tuple(width, height);}};int main(int argc, char** argv){rectangle r(3,4);int w,h;tie(w,h) = r.get_dimensions();cout << w << ' ' << h << '\n';return 0;}

As you can see, it is quite clear that  w  and  h  are  return  values and not parameters. They even get returned with a call to  return . In today’s age with our large code bases I think it is important that we make a move towards readability or we’ll never be able to maintain anything.

Tuple and Tie相关推荐

  1. std::pair std::tuple std::tie

    std::pair 固定包含两个数据,Defined in header <utility> 场景 pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,如stl中的m ...

  2. std::tuple、std::tie(可用于结构体大小比较)、std::pair用法

    1.tuple应用: 解释:是一个元组,可包含无限多不同类型变量,pair的升级版,但没有pair得成员变量first.second. 1.1.代码: // tuple example #includ ...

  3. std::tuple, std::pair, std::tie 快速掌握

    tuple是可以存异构元素的容器,比如可以在一个容器中同时存储int.string.bool等. 作为对比,vector只能存储同类型的元素.tuple可做作为struct的一种更简洁高效的替代. 使 ...

  4. std::tuple类型

    1. 元组简介 std::tuple是类似std::pair的模板.每个pair的成员类型都不相同,但每个pair都恰好有两个成员.不同std::tuple类型的成员类型也不相同,但一个std::tu ...

  5. c++中的ignore和tie

    一.tie和ignore std::tie和std::ignore都是定义在< tuple >这个头文件中的,意思其实就很明了了,它肯定是辅助tuple这个数据结构的,先看一下它们的具体定 ...

  6. C++11 tuple

    C++11 tuple(元组) tuple是C++11中引入的新类型,中文翻译就是元组.元组是一个能够容纳元素集合的对象. 每个元素可以具有不同的类型.类模板std :: tuple是固定大小的异构值 ...

  7. C++元组(tuple)类型

    C++元组tuple类型 介绍 元组tuple是C++的一个模板,不同tuple类型的成员类型也不相同,但是一个tuple可以有任意数量的成员. 每个tuple类型的成员个数是一定的,但是不同的tup ...

  8. 现代c++白皮书(C++11)

    4. C++11:感觉像是门新语言 C++11 [Becker 2011] 发布后,其实现相对来说很快就出现了.这导致了极大的热情,增加了使用,有大量新人涌入 C++ 世界,并进行了大量的实验.C++ ...

  9. 2020-09-11 C++ 11 tuple元组和tie()的简单介绍

    原文 :https://www.cnblogs.com/qicosmos/p/3318070.html 这次要讲的内容是:c++11中的tuple(元组).tuple看似简单,其实它是简约而不简单,可 ...

最新文章

  1. 自然语言处理中句子相似度计算的几种方法
  2. tomcat启动命令行窗口出现乱码的解决方法
  3. 这十大科学文献最烂配图,你可千万别学
  4. 全排列算法的全面解析
  5. Clion 2020.2 使用 C++20 等C++标准 编程
  6. 计算机linux入门教程,Turbolinux入门教程1
  7. mongodb数组更新操作符$push
  8. 电子围栏判断_脉冲电子围栏和张力围栏之间的区别
  9. atitit.提升研发管理的利器---重型框架 框架 类库的区别
  10. python读取cad元素_python3读取autocad图形文件.py实例
  11. 使用bat脚本自动打开cmd并执行命令
  12. 学校计算机教室学生使用记录表,学校学生信息技术教学计划
  13. 【学习笔记】揭秘阿里CDN核心技术
  14. 妙用AccessibilityService黑科技实现微信自动加好友拉人进群聊
  15. 推荐21款最佳 HTML5 网页游戏
  16. 非参数估计:核密度估计KDE
  17. 青少年中医药文化教育功在当代利在千秋
  18. Linux高级运维: Session与Cookie 、 部署memcached和Session共享(OPERATION----DAY36)
  19. java对象为什么要重写equals方法
  20. 计算机系统的位的描述性定义,计算机系统中,“位(bit)”的描述性定义是()

热门文章

  1. Python+OpenCV:仿射变换和透射变换
  2. 关于WM_NOTIFY的使用方法
  3. Java Nested Classes(内部类~第一篇英文技术文档翻译)
  4. 接口测试Fiddler实战
  5. 华为关于gvrp 的配置,一般步骤以及应用。
  6. 控件readonly之后的取值
  7. Matlab APP designer 里的grid on 和hold on
  8. linux 影院 ftp客户端20端口号,Linux下的ftp客户端说明文档
  9. python如何得出数组里最大_如何理解Python里的字典dict?
  10. 性能计数器驱动_Linux CPU性能优化方法