The tuple in tr1 is equivalent to the pair extension in STL. Pair can only contain two elements, while tuple can contain at least 10 elements.
# Include <tr1/tuple>
Using namespace STD: tr1;
Tuple objects can be generated by constructors or through make_tuple and tie functions.
// Constructor
Tuple <int, int> tii (0, 0 );
// Make_tuple
Tuple <int, int> tii = make_tuple (0, 0 );
// Tie
Int I = 0;
Int J = 0;
Tuple <int, int> tii = tie (I, j );
The difference between tie and make_tuple is that all the elements in the tuple object generated by tie are of reference type.
After a tuple object is generated, you can use tuple_size and tuple_element to obtain the number of elements and the type of each element in the tuple object.
Cout <tuple_size <tii>: value; // output 2, because tii contains two elements.
Cout <type_id (tuple_element <0, tii>: type). Name (); // int should be output. The type of The 0th elements is int.
Here, value is a static member of tuple_size, so it can be obtained directly using.
Similarly, type is a static member of tuple_element. Type_id is an operator that helps convert a type into a readable form.
You can use the get function to return a reference to an element in a tuple object. Therefore, you can read and write specific elements in a tuple object.
Get <0> (tii) = 2; // The current tii value is (2, 0)
Get <1> (tii) = 3; // The current tii value is (2, 3.