当前位置:   article > 正文

C++11中的std::tuple_std::tuple_size

std::tuple_size

std::tuple是类似pair的模板。每个pair的成员类型都不相同,但每个pair都恰好有两个成员。不同std::tuple类型的成员类型也不相同,但一个std::tuple可以有任意数量的成员。每个确定的std::tuple类型的成员数目是固定的,但一个std::tuple类型的成员数目可以与另一个std::tuple类型不同。

但我们希望将一些数据组合成单一对象,但又不想麻烦地定义一个新数据结构来表示这些数据时,std::tuple是非常有用的。我们可以将std::tuple看作一个”快速而随意”的数据结构。

当我们定义一个std::tuple时,需要指出每个成员的类型。当我们创建一个std::tuple对象时,可以使用tuple的默认构造函数,它会对每个成员进行值初始化;也可以为每个成员提供一个初始值,此时的构造函数是explicit的,因此必须使用直接初始化方法。类似make_pair函数,标准库定义了make_tuple函数,我们还可以使用它来生成std::tuple对象。类似make_pair,make_tuple函数使用初始值的类型来推断tuple的类型。

一个std::tuple类型的成员数目是没有限制的,因此,tuple的成员都是未命名的。要访问一个tuple的成员,就要使用一个名为get的标准库函数模板。为了使用get,我们必须指定一个显式模板实参,它指出我们想要访问第几个成员。我们传递给get一个tuple对象,它返回指定成员的引用。get尖括号中的值必须是一个整型常量表达式。与往常一样,我们从0开始计数,意味着get<0>是第一个成员。

为了使用tuple_size或tuple_element,我们需要知道一个tuple对象的类型。与往常一样,确定一个对象的类型的最简单方法就是使用decltype。

std::tuple的关系和相等运算符的行为类似容器的对应操作。这些运算符逐对比较左侧tuple和右侧tuple的成员。只有两个tuple具有相同数量的成员时,我们才可以比较它们。而且,为了使用tuple的相等或不等运算符,对每对成员使用==运算符必须都是合法的;为了使用关系运算符,对每对成员使用<必须都是合法的。由于tuple定义了<和==运算符,我们可以将tuple序列传递给算法,并且可以在无序容器中将tuple作为关键字类型。

std::tuple的一个常见用途是从一个函数返回多个值。

std::tuple是一个模板,允许我们将多个不同类型的成员捆绑成单一对象。每个tuple包含指定数量的成员,但对一个给定的tuple类型,标准库并未限制我们可以定义的成员数量上限。

std::tuple中元素是被紧密地存储的(位于连续的内存区域),而不是链式结构。

std::tuple实现了多元组,这是一个编译期就确定大小的容器,可以容纳不同类型的元素。多元组类型在当前标准库中被定义为可以用任意数量参数初始化的类模板。每一模板参数确定多元组中一元素的类型。所以,多元组是一个多类型、大小固定的值的集合。

  1. #include "tuple.hpp"
  2. #include <iostream>
  3. #include <tuple>
  4. #include <string>
  5. #include <functional>
  6. #include <utility>
  7. //
  8. // reference: http://www.cplusplus.com/reference/tuple/tuple/
  9. int test_tuple_4()
  10. {
  11. { // tuple::tuple: Constructs a tuple object. This involves individually constructing its elements,
  12. // with an initialization that depends on the constructor form invoke
  13. std::tuple<int, char> first; // default
  14. std::tuple<int, char> second(first); // copy
  15. std::tuple<int, char> third(std::make_tuple(20, 'b')); // move
  16. std::tuple<long, char> fourth(third); // implicit conversion
  17. std::tuple<int, char> fifth(10, 'a'); // initialization
  18. std::tuple<int, char> sixth(std::make_pair(30, 'c')); // from pair / move
  19. std::cout << "sixth contains: " << std::get<0>(sixth);
  20. std::cout << " and " << std::get<1>(sixth) << '\n';
  21. }
  22. { // std::tuple::operator=: Each of the elements in the tuple object is assigned its corresponding element
  23. std::pair<int, char> mypair(0, ' ');
  24. std::tuple<int, char> a(10, 'x');
  25. std::tuple<long, char> b, c;
  26. b = a; // copy assignment
  27. c = std::make_tuple(100L, 'Y'); // move assignment
  28. a = c; // conversion assignment
  29. c = std::make_tuple(100, 'z'); // conversion / move assignment
  30. a = mypair; // from pair assignment
  31. a = std::make_pair(2, 'b'); // form pair /move assignment
  32. std::cout << "c contains: " << std::get<0>(c);
  33. std::cout << " and " << std::get<1>(c) << '\n';
  34. }
  35. { // std::tuple::swap: Exchanges the content of the tuple object by the content of tpl,
  36. // which is another tuple of the same type (containing objects of the same types in the same order)
  37. std::tuple<int, char> a(10, 'x');
  38. std::tuple<int, char> b(20, 'y');
  39. a.swap(b);
  40. std::cout << "a contains: " << std::get<0>(a);
  41. std::cout << " and " << std::get<1>(a) << '\n';
  42. std::swap(a, b);
  43. std::cout << "a contains: " << std::get<0>(a);
  44. std::cout << " and " << std::get<1>(a) << '\n';
  45. }
  46. { // std::relational operators: Performs the appropriate comparison operation between the tuple objects lhs and rhs
  47. std::tuple<int, char> a(10, 'x');
  48. std::tuple<char, char> b(10, 'x');
  49. std::tuple<char, char> c(10, 'y');
  50. if (a == b) std::cout << "a and b are equal\n";
  51. if (b != c) std::cout << "b and c are not equal\n";
  52. if (b<c) std::cout << "b is less than c\n";
  53. if (c>a) std::cout << "c is greater than a\n";
  54. if (a <= c) std::cout << "a is less than or equal to c\n";
  55. if (c >= b) std::cout << "c is greater than or equal to b\n";
  56. }
  57. return 0;
  58. }
  59. // reference: https://msdn.microsoft.com/en-us/library/bb982771.aspx
  60. int test_tuple_3()
  61. {
  62. typedef std::tuple<int, double, int, double> Mytuple;
  63. Mytuple c0(0, 1, 2, 3);
  64. // display contents " 0 1 2 3"
  65. std::cout << " " << std::get<0>(c0);
  66. std::cout << " " << std::get<1>(c0);
  67. std::cout << " " << std::get<2>(c0);
  68. std::cout << " " << std::get<3>(c0);
  69. std::cout << std::endl;
  70. Mytuple c1;
  71. c1 = c0;
  72. // display contents " 0 1 2 3"
  73. std::cout << " " << std::get<0>(c1);
  74. std::cout << " " << std::get<1>(c1);
  75. std::cout << " " << std::get<2>(c1);
  76. std::cout << " " << std::get<3>(c1);
  77. std::cout << std::endl;
  78. std::tuple<char, int> c2(std::make_pair('x', 4));
  79. // display contents " x 4"
  80. std::cout << " " << std::get<0>(c2);
  81. std::cout << " " << std::get<1>(c2);
  82. std::cout << std::endl;
  83. Mytuple c3(c0);
  84. // display contents " 0 1 2 3"
  85. std::cout << " " << std::get<0>(c3);
  86. std::cout << " " << std::get<1>(c3);
  87. std::cout << " " << std::get<2>(c3);
  88. std::cout << " " << std::get<3>(c3);
  89. std::cout << std::endl;
  90. typedef std::tuple<int, float, int, float> Mytuple2;
  91. Mytuple c4(Mytuple2(4, 5, 6, 7));
  92. // display contents " 4 5 6 7"
  93. std::cout << " " << std::get<0>(c4);
  94. std::cout << " " << std::get<1>(c4);
  95. std::cout << " " << std::get<2>(c4);
  96. std::cout << " " << std::get<3>(c4);
  97. std::cout << std::endl;
  98. return (0);
  99. }
  100. ///
  101. // reference: http://zh.cppreference.com/w/cpp/utility/tuple
  102. static std::tuple<double, char, std::string> get_student(int id)
  103. {
  104. if (id == 0) return std::make_tuple(3.8, 'A', "Lisa Simpson");
  105. if (id == 1) return std::make_tuple(2.9, 'C', "Milhouse Van Houten");
  106. if (id == 2) return std::make_tuple(1.7, 'D', "Ralph Wiggum");
  107. throw std::invalid_argument("id");
  108. }
  109. int test_tuple_2()
  110. {
  111. auto student0 = get_student(0);
  112. std::cout << "ID: 0, "
  113. << "GPA: " << std::get<0>(student0) << ", "
  114. << "grade: " << std::get<1>(student0) << ", "
  115. << "name: " << std::get<2>(student0) << '\n';
  116. double gpa1;
  117. char grade1;
  118. std::string name1;
  119. std::tie(gpa1, grade1, name1) = get_student(1);
  120. std::cout << "ID: 1, "
  121. << "GPA: " << gpa1 << ", "
  122. << "grade: " << grade1 << ", "
  123. << "name: " << name1 << '\n';
  124. return 0;
  125. }
  126. ///
  127. // reference: http://www.cplusplus.com/reference/tuple/
  128. static void print_pack(std::tuple<std::string&&, int&&> pack)
  129. {
  130. std::cout << std::get<0>(pack) << ", " << std::get<1>(pack) << '\n';
  131. }
  132. static void fun(int &a)
  133. {
  134. a = 15;
  135. }
  136. int test_tuple_1()
  137. {
  138. { // std::tuple_element: class template, Class designed to access the type of the Ith element in a tuple.
  139. // It is a simple class with a single member type, tuple_element::type,
  140. // defined as an alias of the type of the Ith element in a tuple of type T.
  141. auto mytuple = std::make_tuple(10, 'a');
  142. std::tuple_element<0, decltype(mytuple)>::type first = std::get<0>(mytuple);
  143. std::tuple_element<1, decltype(mytuple)>::type second = std::get<1>(mytuple);
  144. std::cout << "mytuple contains: " << first << " and " << second << '\n';
  145. }
  146. { // std::tuple_size: Class template designed to access the number of elements in a tuple
  147. std::tuple<int, char, double> mytuple(10, 'a', 3.14);
  148. std::cout << "mytuple has ";
  149. std::cout << std::tuple_size<decltype(mytuple)>::value;
  150. std::cout << " elements." << '\n';
  151. }
  152. { // std::forward_as_tuple: function template, Constructs a tuple object with rvalue references
  153. // to the elements in args suitable to be forwarded as argument to a function.
  154. std::string str("John");
  155. print_pack(std::forward_as_tuple(str + " Smith", 25));
  156. print_pack(std::forward_as_tuple(str + " Daniels", 22));
  157. }
  158. { // std::get: funtion template, Returns a reference to the Ith element of tuple tpl.
  159. std::tuple<int, char> mytuple(10, 'a');
  160. std::get<0>(mytuple) = 20;
  161. std::cout << "mytuple contains: ";
  162. std::cout << std::get<0>(mytuple) << " and " << std::get<1>(mytuple);
  163. std::cout << std::endl;
  164. }
  165. { // std::make_tuple: function template, Constructs an object of the appropriate tuple type
  166. // to contain the elements specified in args
  167. auto first = std::make_tuple(10, 'a'); // tuple < int, char >
  168. const int a = 0; int b[3]; // decayed types:
  169. auto second = std::make_tuple(a, b); // tuple < int, int* >
  170. auto third = std::make_tuple(std::ref(a), "abc"); // tuple < const int&, const char* >
  171. std::cout << "third contains: " << std::get<0>(third);
  172. std::cout << " and " << std::get<1>(third);
  173. std::cout << std::endl;
  174. }
  175. { // std::tie: function template, Constructs a tuple object whose elements are references
  176. // to the arguments in args, in the same order
  177. // std::ignore: object, This object ignores any value assigned to it. It is designed to be used as an
  178. // argument for tie to indicate that a specific element in a tuple should be ignored.
  179. int myint;
  180. char mychar;
  181. std::tuple<int, float, char> mytuple;
  182. mytuple = std::make_tuple(10, 2.6, 'a'); // packing values into tuple
  183. std::tie(myint, std::ignore, mychar) = mytuple; // unpacking tuple into variables
  184. std::cout << "myint contains: " << myint << '\n';
  185. std::cout << "mychar contains: " << mychar << '\n';
  186. }
  187. { // std::tuple_cat: function template, Constructs an object of the appropriate tuple type
  188. // to contain a concatenation of the elements of all the tuples in tpls, in the same order
  189. std::tuple<float, std::string> mytuple(3.14, "pi");
  190. std::pair<int, char> mypair(10, 'a');
  191. auto myauto = std::tuple_cat(mytuple, std::tuple<int, char>(mypair));
  192. std::cout << "myauto contains: " << '\n';
  193. std::cout << std::get<0>(myauto) << '\n';
  194. std::cout << std::get<1>(myauto) << '\n';
  195. std::cout << std::get<2>(myauto) << '\n';
  196. std::cout << std::get<3>(myauto) << '\n';
  197. }
  198. { // tuple::tuple: A tuple is an object capable to hold a collection of elements.
  199. // Each element can be of a different type.
  200. std::tuple<int, char> foo(10, 'x');
  201. auto bar = std::make_tuple("test", 3.1, 14, 'y');
  202. std::get<2>(bar) = 100; // access element
  203. int myint; char mychar;
  204. std::tie(myint, mychar) = foo; // unpack elements
  205. std::tie(std::ignore, std::ignore, myint, mychar) = bar; // unpack (with ignore)
  206. mychar = std::get<3>(bar);
  207. std::get<0>(foo) = std::get<2>(bar);
  208. std::get<1>(foo) = mychar;
  209. std::cout << "foo contains: ";
  210. std::cout << std::get<0>(foo) << ' ';
  211. std::cout << std::get<1>(foo) << '\n';
  212. }
  213. {
  214. std::tuple<int, char> foo{ 12, 'a' };
  215. std::cout << std::get<0>(foo) << "\n"; // 12
  216. fun(std::get<0>(foo));
  217. std::cout << std::get<0>(foo) << "\n"; // 15
  218. }
  219. return 0;
  220. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/305967
推荐阅读
相关标签
  

闽ICP备14008679号