The most essential difference between a class tuple and an array is when the number of tuple tuple element types can be different, and the element type of the uniform array array must be the same.
This article mainly examples:
tuple_size
Example
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
|
//Tuple Example#include <iostream>//Std::cout#include <tuple>//Std::tuple, Std::get, Std::tie, Std::ignoreintMain () {std::tuple<int,char> foo (10,‘x‘);autoBar = Std::make_tuple ("test", 3.1, 14,‘y‘); Std::get<2> (bar) = 100;//Access element intMyintcharMyChar; Std::tie (myint, MyChar) = foo;//Unpack elementsStd::tie (Std::ignore, Std::ignore, myint, mychar) = bar;//unpack (with ignore)MyChar = std::get<3> (bar); Std::get<0> (foo) = std::get<2> (bar); Std::get<1> (foo) = MyChar; Std::cout <<"foo contains: "; Std::cout << std::get<0> (foo) <<‘ ‘; Std::cout << std::get<1> (foo) <<‘\n‘;return0;}
|
Edit & Run |
Output:
Tie----(unpack)
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// packing/unpacking tuples#include <iostream> // std::cout#include <tuple> // std::tuple, std::make_tuple, std::tieint main (){ int myint; char mychar; std::tuple<int,float,char> mytuple; ‘a‘); // packing values into tuple std::tie (myint, std::ignore, mychar) = mytuple; // unpacking tuple into variables "myint contains: "‘\n‘; "mychar contains: "‘\n‘; return 0;}
|
Edit & Run |
Output:
myint contains: 10mychar contains: a |
std:tuple_elementMember types
member Type |
definition |
type |
The I th type in the tuple object |
Example
1 2 3 4 5 6 7 8 9 One + All |
//Tuple_element #include <iostream> //std::cout #include <tuple> // Std::tuple, Std::make_tuple, std::tuple_element, std::get int main () { auto mytuple = std:: Make_tuple (Ten, ' a ' ); Std::tuple_element<0, decltype (mytuple) >::type first = std::get<0> (mytuple); Std::tuple_element<1, decltype (mytuple) >::type second = std::get<1> (mytuple); Std::cout << mytuple contains: << first << "and" << second <<
' \ n ' ;
return 0;}
|
edit & Run |
Output:
mytuple contains: 10 and a |
tuple_size
Member Constants
member Constant |
definition |
Value |
The number of elements in the tuple or Tuple-like object. This is a constexpr value of the unsigned integral type size_t . |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// tuple_size#include <iostream> // std::cout#include <tuple> // std::tuple, std::tuple_sizeint main (){ std::tuple<int,char,double> mytuple (10,‘a‘,3.14); "mytuple has "; std::cout << std::tuple_size<decltype(mytuple)>::value; " elements."‘\n‘; return 0;}
|
Edit & Run |
Output:
forward_as_tuple
Parameters
-
Args
-
List of elements to be forwarded as a tuple object of references.
Return Value
A tuple object with rvalue references to args suitable to is forwarded as argument to a function.
Example
1 2 3 4 5 6 7 8 9 Ten All |
//forward_as_tuple example #include < iostream> //std::cout #include <tuple> //Std::tuple, Std::get, St D::forward_as_tuple #include <string> //std::string void print_pack (Std::tuple<std::string&&, int &&> pack) {std::cout << std::get<0> (Pack) << "," << std::get<1> (Pack) << ' \ n ' ;} int Main () {std::string str ( "John" ); Print_pack (Std::forward_as_tuple (str+ "Smith" , 25)); Print_pack (Std::forward_as_tuple (str+ "Daniels" , 22));
return 0;} |
edit & Run |
Output:
John Smith, 25John Daniels, 22 |
tuple_cat
Parameters
-
Tpls
-
comma-separated List of tuple objects. These may is of different types.
Return Value
A tuple object of the appropriate type to hold args .
The type of the returned object ( tuple<CTypes...>
) is the type, contains the ordered sequence of all the extended types in< tuple c10/>.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
//Tuple_cat Example#include <iostream>//Std::cout#include <utility>//std::p air#include <string>//std::string#include <tuple>//Std::tuple, Std::tuple_cat, Std::getintMain () {std::tuple<float,std::string> mytuple (3.14,"pi"); std::p air<int,char> Mypair (10,‘a‘);autoMyauto = Std::tuple_cat (Mytuple, std::tuple<int,char> (Mypair)); Std::cout <<"myauto contains: "<<‘\n‘; Std::cout << std::get<0> (myauto) <<‘\n‘; Std::cout << std::get<1> (myauto) <<‘\n‘; Std::cout << std::get<2> (myauto) <<‘\n‘; Std::cout << std::get<3> (myauto) <<‘\n‘;return0;}
|
Edit & Run |
Output:
myauto contains:3.14pi10a |
Ignore tie
Parameters
-
Args
-
List of objects (
lvalues) to is tied as elements of a tuple .
Return Value
A tuple with lvalue references to args .
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// packing/unpacking tuples#include <iostream> // std::cout#include <tuple> // std::tuple, std::make_tuple, std::tieint main (){ int myint; char mychar; std::tuple<int,float,char> mytuple; ‘a‘); // packing values into tuple std::tie (myint, std::ignore, mychar) = mytuple; // unpacking tuple into variables "myint contains: "‘\n‘; "mychar contains: "‘\n‘; return 0;}
|
Edit & Run |
Output:
myint contains: 10mychar contains: a |
Tuple tuples (c++11 and later, such as C++14)