C++11 标准新引入了一种类模板,命名为 tuple(中文可直译为元组)。tuple 最大的特点是:实例化的对象可以存储任意数量、任意类型的数据。
tuple 的应用场景很广泛,例如当需要存储多个不同类型的元素时,可以使用 tuple;当函数需要返回多个数据时,可以将这些数据存储在 tuple 中,函数只需返回一个 tuple 对象即可。
tuple对象的创建
tuple 本质是一个以可变模板参数定义的类模板,它定义在 <tuple> 头文件并位于 std 命名空间中。因此要想使用 tuple 类模板,程序中需要首先引入以下代码:
1 2
   | #include <tuple> using std::tuple;
   | 
 
实例化 tuple 模板类对象常用的方法有两种,一种是借助该类的构造函数,另一种是借助 make_tuple() 函数。
类的构造函数
tuple 模板类提供有很多构造函数,包括:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
   |  constexpr tuple();
 
  tuple (const tuple& tpl);
 
  tuple (tuple&& tpl);
 
  template <class... UTypes>     tuple (const tuple<UTypes...>& tpl);  template <class... UTypes>     tuple (tuple<UTypes...>&& tpl);      
 
  explicit tuple (const Types&... elems);   template <class... UTypes>     explicit tuple (UTypes&&... elems);  
 
  template <class U1, class U2>     tuple (const pair<U1,U2>& pr);        template <class U1, class U2>     tuple (pair<U1,U2>&& pr);            
 
  | 
 
举个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13
   | #include <iostream>     // std::cout #include <tuple>        // std::tuple using std::tuple; int main() {     std::tuple<int, char> first;                                  std::tuple<int, char> second(first);                          std::tuple<int, char> third(std::make_tuple(20, 'b'));        std::tuple<long, char> fourth(third);                         std::tuple<int, char> fifth(10, 'a');                         std::tuple<int, char> sixth(std::make_pair(30, 'c'));         return 0; }
   | 
 
make_tuple()函数
上面程序中,我们已经用到了 make_tuple() 函数,它以模板的形式定义在 <tuple> 头文件中,功能是创建一个 tuple 右值对象(或者临时对象)。
对于 make_tuple() 函数创建了 tuple 对象,我们可以上面程序中那样作为移动构造函数的参数,也可以这样用:
1 2 3
   | auto first = std::make_tuple (10,'a');    const int a = 0; int b[3]; auto second = std::make_tuple (a,b);     
   | 
 
程序中分别创建了 first 和 second 两个 tuple 对象,它们的类型可以直接用 auto 表示。
tuple常用函数
| 函数或类模板 | 
描 述 | 
tup1.swap(tup2) swap(tup1, tup2) | 
tup1 和 tup2 表示类型相同的两个 tuple 对象,tuple 模板类中定义有一个 swap() 成员函数,<tuple> 头文件还提供了一个同名的 swap() 全局函数。  swap() 函数的功能是交换两个 tuple 对象存储的内容。 | 
get<num>(tup) | 
tup 表示某个 tuple 对象,num 是一个整数,get() 是 <tuple> 头文件提供的全局函数,功能是返回 tup 对象中第 num+1 个元素。 | 
tuple_size<type>::value | 
tuple_size 是定义在 <tuple> 头文件的类模板,它只有一个成员变量 value,功能是获取某个 tuple 对象中元素的个数,type 为该tuple 对象的类型。 | 
tuple_element<I, type>::type | 
tuple_element 是定义在 <tuple> 头文件的类模板,它只有一个成员变量 type,功能是获取某个 tuple 对象第 I+1 个元素的类型。 | 
forward_as_tuple<args...> | 
args… 表示 tuple 对象存储的多个元素,该函数的功能是创建一个 tuple 对象,内部存储的 args… 元素都是右值引用形式的。 | 
tie(args...) = tup | 
tup 表示某个 tuple 对象,tie() 是 <tuple> 头文件提供的,功能是将 tup 内存储的元素逐一赋值给 args… 指定的左值变量。 | 
tuple_cat(args...) | 
args… 表示多个 tuple 对象,该函数是 <tuple> 头文件提供的,功能是创建一个 tuple 对象,此对象包含 args… 指定的所有 tuple 对象内的元素。 | 
tuple 模板类对赋值运算符 = 进行了重载,使得同类型的 tuple 对象可以直接赋值。此外,tuple 模板类还重载了 ==、!=、<、>、>=、<= 这几个比较运算符,同类型的 tuple 对象可以相互比较(逐个比较各个元素)。
下面的程序演示了表 1 中一部分函数模板和类模板的功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
   | #include <iostream> #include <tuple>
  int main() {     int size;          std::tuple<int, char> mytuple(10, 'x');          size = std::tuple_size<decltype(mytuple)>::value;          std::cout << std::get<0>(mytuple) << " " << std::get<1>(mytuple) << std::endl;          std::get<0>(mytuple) = 100;     std::cout << std::get<0>(mytuple) << std::endl;          auto bar = std::make_tuple("test", 3.1, 14);          const char* mystr = nullptr;     double mydou;     int myint;          std::tie(mystr, mydou, myint) = bar;               auto mycat = std::tuple_cat(mytuple, bar);     size = std::tuple_size<decltype(mycat)>::value;     std::cout << size << std::endl;     return 0; }
   | 
 
程序执行结果为:
tuple函数返回多个值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
   | #include <tuple> // tuple头文件 #include <stdio.h> #include <string> using namespace std;  
  tuple<int, string> foo();   int main() {          int a;     string b;            tie(a, b) = foo();     printf("%d => %s\n", a, b.c_str());                 tuple<int, string, char, float> x = make_tuple(2014, "tupule", 'x', 5.30);     printf("%d, %s, %c, %.2f\n", get<0>(x), get<1>(x).c_str(), get<2>(x), get<3>(x));          return 0; }   tuple<int, string> foo() {          return make_tuple(2014, "tuple"); }
   | 
 
转载:http://c.biancheng.net/view/8600.html