tuple c++

A tuple is a C++11 construction and it is built heavily on variadic templates.

A tuple is a variadic class template that stores an unlimited set of values of different types, defined when instantiating the tuple; for example:

tuple<int, int> x;</int,>

will store 2 integers.

tuple<string, int, bool> y;</string,>

will store one string, one integer and one boolean and so on.

Tuples are useful for several things:

  • They are light replacements for structures.

  • They can be useful to return several values from a function

  • They let you perform comparisons through all the value set

You can instantiate tuples in these ways:

tuple<int, int, bool> x; //instantiating but not initializing<br>tuple<int, string> y { 2, "hello" }; //instantiating AND initializing<br>auto z = make_tuple(2, 3, 4, "bye"s); //instantiating AND initializing throught the make_tuple helper function.</int,></int,>

Consider you declare something like this:

auto xx = make_tuple(3.14, "PI"s);

To get the tuple values, you must to use the get<int>()</int> function like in this code excerpt:

The template parameter is the index of the element to be retrieved, so, if the first element was an integer, the index 0 will retrieve the integer stored as the first value in the tuple, the index 0 will retrieve the second one and so on.

Tuples are also very useful to create structs “on the fly” and to use them in data structures; for example:

In this case I did not need to create a new struct in order to store the number and the number name, for example.

Other nice thing on tuples is that you can compare tuples relying on the comparison operators of the data types inside the tuples.

Look at this code:

When it is executed, this thing is displayed:

BMW, X5; 2014<br>Chevrolet, Sonic; 2013<br>Toyota, Rav4; 2012<br>VW, Jetta; 2014<br>VW, Jetta; 2015<br>******<br>Toyota, Rav4; 2012

The code and its execution contain several interesting things:

  • I defined an alias to a tuple called “car”, having it, I was able to use the tuple as a declared type in a lot of cases… without declaring it!

  • If I would have created a “car” struct, to use it inside a set, I would have to overload the operator

Now I want to modify my set in order to store the elements ordered by year, by brand and by model. Look at the new implementation:

The output is:

Toyota, Rav4; 2012<br>Chevrolet, Sonic; 2013<br>BMW, X5; 2014<br>VW, Jetta; 2014<br>VW, Jetta; 2015<br>******<br>Toyota, Rav4; 2012

The elements are ordered first by year.

Notice my “cc” lambda expression. It uses a function called “tie”.

“tie” is a function that takes a set of references and creates a tuple containing references (not values or copies) to the original values. So, tie is useful to create temporary light tuples. In my example, using “tie”, I was able to create other set of tuples with different logical order, so the tuple operator

Original: https://www.cnblogs.com/bigben0123/p/15509620.html
Author: Bigben
Title: tuple c++

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/536219/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • C++/服务器开发4天实战训练营

    第一天: 1.四种不同的方式来实现add函数 //面向过程 int add1(int a, int b) { return a + b; } //面向对象 class ADD{ p…

    C++ 2023年5月29日
    045
  • C++实现二次、三次B样条曲线

    下面是一个有四个控制点的Bezier 曲线: 可以通过改变一个控制点的位置来改变曲线的形状,比如将上图曲线中左边第二个控制点往上移,就可以得到下面的曲线: 可以看到,这种曲线生成方…

    C++ 2023年5月29日
    040
  • 收藏的博客 — Qt/C++学习

    Qt Creator环境: 使用Qt Creator作为Linux IDE,代替Vim:实现两台Linux电脑远程部署和调试(一台电脑有桌面系统,一台电脑无桌面系统) 使用Qt C…

    C++ 2023年5月29日
    045
  • 选择排序 C++实现

    实现思想: 1.寻找[i, n)区间里的最小值min ( i>= 0 ) 2.交换min和第i的数 ( i>= 0 ) #include #include using …

    C++ 2023年5月29日
    058
  • 玩转cocos2d-x lua-binding, 实现c++与lua混合编程

    引言 城市精灵GO(http://csjl.teamtop3.com/)是一款基于cocos2d-x开发的LBS社交游戏, 通过真实地图的探索, 发现和抓捕隐匿于身边的野生精灵, …

    C++ 2023年5月29日
    052
  • How to: Create a C/C++ Union by Using Attributes (C#)

    【 How to: Create a C/C++ Union by Using Attributes (C#)】 1、you can create what is known as…

    C++ 2023年5月29日
    062
  • 汉诺塔的c++实现

    void hanNuoTa(int n,int a,int b,int c) { if (n == 0) return; hanNuoTa(n – 1, a, c, b); cou…

    C++ 2023年5月29日
    060
  • C++函数模板template(模板函数)

    函数模板不是一个实在的函数,编译器不能为其生成可执行代码。定义函数模板后只是一个对函数功能框架的描述,当它具体执行时,将根据传递的实际参数决定其功能。 面向对象的继承和多态机制有效…

    C++ 2023年5月29日
    054
  • C++接口设计和代码重构

    讲了接口设计, 文档(doxygen),测试等方面。对于从头编写基础库, 或者没有基础库开发经验并且需要短期内上手, 有一定实际参考价值。 所谓Mikado Method方法是用来…

    C++ 2023年5月29日
    052
  • CLion之C++框架篇-优化框架,引入boost(三)

    背景 结合上一篇CLion之C++框架篇-优化框架,单元测试(二) ,继续进行框架优化!这一版优化引入一个我们日常经常使用的操作库Boost,估算使用频率在70%以上! Boost…

    C++ 2023年5月29日
    076
  • 聊聊 C++ 中的几种智能指针 (上)

    一:背景 我们知道 C++ 是手工管理内存的分配和释放,对应的操作符就是 new/delete 和 new[] / delete[], 这给了程序员极大的自由度也给了我们极高的门槛…

    C++ 2023年5月29日
    083
  • C++map值排序

    class Solution { public: static bool cmp(pair a, pair b){ return a.second>b.second; } s…

    C++ 2023年5月29日
    040
  • C++ d-bus 使用实例

    因为以后工作用的到D-bus,这两天抽空看了下c++下得d-bus的使用方法。因为网上对c++下得d-bus使用说明几乎没有,所以,在这里记录下以供同仁使用。同时感谢shengpe…

    C++ 2023年5月29日
    058
  • C/C++标准新特性简介

    参考文档 C语言的起源发展 C语言诞生于1972年,美国贝尔实验室。作者为:Dennis MacAlistair Ritchie(丹尼斯·里奇) & Kenneth Lan…

    C++ 2023年5月29日
    080
  • ProtoBuf3 C++使用篇

    protobuf 是用于结构化数据串行化的灵活、高效、自动化的解决方案。又如 XML,不过它更小、更快、也更简单。你只需要按照你想要的数据存储格式编写一个.proto,然后使用生成…

    C++ 2023年5月29日
    051
  • 使用VS2015进行C++开发的6个主要原因

    使用VS2015进行C++开发的6个主要原因 使用Visual Studio 2015进行C++开发 在今天的 Build 大会上,进行了”将你的 C++ 代码转移至 …

    C++ 2023年5月29日
    066
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球