directory
1、tuple
Tuple extends Pair to see the function of two elements as a unit, and realize the function that can treat any number of elements as a unit.
2, function
tuple <T1, t2 ... tn> t; Create Tuple with N given elements
Tuple <T1, t2 ... tn> t (v1, v2 ... vn); build tuple and initialize
Tuple <T1, T2> T (PAIR); Create a TPLE, with two elements
t = t2; assign the value t2 to T
t = Pair; assign pair to TPLE with two elements
T1 == T2; compare all elements
T1! = T2
T1 <T2
T1> T2
T1 <= t2
t1> = t2
t1.swap (t2);
swap (t1, t2);
make_tuple (v1, v2 ...); build Tuple to pass the type
tie (ref (1) ...; build a tuple composed of reference
3, test
#include <iOSTREAM>
#include <Tuple>
#include <string>
using namespace std;
template <th IDX, int Max, Typename ... ARGS>
struct Print_tuple {
Static Void Print (OSTREAM & OS, Const Tuple <args ...> & T)
{{
OS << get <idx> (t) << (IDX + 1 == max? "": ":", ");
// recursion
Print_tuple <IDX + 1, MAX, ARGS ...> :: Print (OS, T);
}
};
// End of recursion
template <th Max, Typename ... ARGS>
struct print_tuple <max, max, args ...>
{{
Static Void Print (OSTREAM & OS, Const Tuple <args ...> & T)
{{
}
};
// Reight -load operation operating symbols <<
template <typename ... args>
OSTREAM & Operator << (OSTREAM & OS, Const Tuple <args ...> & T)
{{
os << "[";;
Print_tuple <0, sizeof ... (args), args ...> :: Print (OS, T);
Return os << "];;
}
int Main ()
{{
// Create
tuple <int, char, string> t;
t = Make_tuple (1, 'a', "abc");
COUT << Get <0> (t) << ENDS << GET <1> (t) << ENDS <2> (t) .c_str () << Endl;
// Take your value
int a = 0;
char ch1 = '';
String Str;
tie (a, ch1, str) = t;
copy << ENDS << ch1 << ENDS << Str.c_str () << Endl;
// This operation is not allowed, and implicit conversion is not allowed
// vector <Tuple <int, char, string >> v {{1, 'a', "abc"}, {2, 'b', "def"}};
core << t << Endl;
// Get the element type
tuple_element <0, decltype (t)> :: Type ages;
// Get the number of elements
COUT << Tuple_size <Deckype (T)> :: Value << Endl;
System ("pause");
}