赞
踩
参考原文地址:
https://blog.csdn.net/chayalikemei/article/details/51105630
先上tuple使用的测试程序:
// 元组访问
std::tuple<int, int, vector<int>> tupleTest(1, 4, { 5,6,7,8 });
// 元组个数
int tupleTestSize = std::tuple_size<decltype(tupleTest)>::value;
cout << "tuple size is : " << tupleTestSize << endl;
auto first = get<0>(tupleTest);
cout << "first value is : " << first << endl;
auto second = get<1>(tupleTest);
cout << "second value is : " << second << endl;
auto third = get<2>(tupleTest);
for (const auto & iter : third)
{
cout << "third value is : " << iter << endl;
}
auto catTupleTest = make_tuple(1, 4);
// 元组合并
auto newTuple = std::tuple_cat(catTupleTest, tupleTest);
int newTupleSize = std::tuple_size<decltype(newTuple)>::value;
cout << "after cat, new tuple size is : " << newTupleSize << endl;
运行结果:
tuple size is : 3
first value is : 1
second value is : 4
third value is : 5
third value is : 6
third value is : 7
third value is : 8
after cat, new tuple size is : 5
tuple<int,float,size_t> threet;
tuple<size_t,size_t> tup={1,2,3};//错误
tuple<string,vector<double>,int,list<int>> someVal("tuple",{2.14,3.15},100,{1,2,3})//正确
tuple<int,int,double> some{2,3,3.15};//这样也行,注意与上面的括号
auto itme=make_tuple("string",3,20.01);
//item的类型为tuple<const char*,int,double>
auto itme = make_tuple("string", 3, 20.01);
auto book = get<1>(itme);//返回tuple对象item的第二个成员,从0开始计数
auto itme = make_tuple("string", 3, 20.01);
auto sz = tuple_size<decltype(itme)>::value;//sz为3
auto itme = make_tuple("string", 3, 20.01);
tuple_element<1, decltype(itme)>::type ctype;//ctype的类型为int
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。