初始化
//<学号,姓名,年龄>
tuple<unsigned, string, unsigned> t1; //t1 = (0, "", 0)
tuple<unsigned, string, unsigned> t2(1, "张三", 18); //t2 = (1, "张三", 18)
auto t3 = make_tuple(2, "李四", 21); //t3 = (2, "李四", 21)
其他功能
关系运算符
bool s1 = (t2 == t1); //s1 = false
bool s2 = (t2 != t1); //s2 = true
bool s3 = (t2 < t1); //s3 = false
bool s4 = (t2 > t1); //s4 = true
随机访问;尖括号的值必须是常量表达式;传入的值是左值,返回左值引用,否则,返回右值引用。
auto id = get<0>(t2); //id = 1
auto name = get<1>(t2); //name = "张三"
auto age = get<2>(t2); //age = 18
重命名t2的类型,两种方法,前者为C++11新规
//using type = decltype(t2);
typedef tuple<unsigned, string, unsigned> type;
获取成员数量
size_t s5 = tuple_size<type>::value; //s5 = 3
获取某个成员的类型
tuple_element<1, type>::type name2 = get<1>(t2); //name2 = "张三"
详细内容请看C++Primer第17章 17.1 部分
下载C++Primer第五版