元组tuple是C++的一个模板,不同tuple类型的成员类型也不相同,但是一个tuple可以有任意数量的成员。
每个tuple类型的成员个数是一定的,但是不同的tuple成员个数可以不同。
tuple的定义及初始化使用tuple要引入tuple头文件
#include
tuple定义以及初始化(我们这里以成员类型为int、string、vector为例子)
tuple> test{1,"hello,world",{4,5,6}};
当然也可以使用它的构造函数进行初始化
tuple> test(1,"hello,world",{4,5,6});
存在这种情况,表面上看起来没有进行任何初始化操作,但实际上利用了不带参数的默认构造函数
tupletest; //每个成员的值为0
当然如果你这样定义后,又想对test的初始值所作更改,可以这样写,如下所示。(实际上现在的test不是原来的test,它被你新创建的tuple对象所替代了,严格意义来讲并不算是初始化)
tupletest; //每个成员的值为0 test = tuple (1,2); //第一个成员的值为1,第二个成员的值为2
另外标准库定义了make_tuple函数来生成tuple对象,我们可以用auto关键字来推测生成的tuple的类型
auto test2 = make_tuple(3.14,"wasd");
如果要添加一个vector作为成员,则情况如下
//错误写法
auto test2 = make_tuple(3.14,"wasd",{ 3,4,5 });
//正确写法
vector nums = { 3,4,5 };
auto test2 = make_tuple(3.14,"wasd",nums);
tuple的使用
成员访问
说起tuple的使用,最能直接想到的自然是对tuple的访问了。
在C++标准库里,有一个名为get的函数模板。为了使用get,我们必须指定一个显式模板实参来指示访问的第几个成员,并在函数参数中给予它一个tuple对象。
tuple> test{ 1,"hello,world",{4,5,6} }; cout << get<0>(test) << endl; //打印test第一个成员,其类型为int cout << get<1>(test) << endl; //打印test第二个成员,其类型为string cout << get<2>(test)[0] << endl; //打印test第三个成员vector 的第一个元素
下面是返回结果
1 hello,world 4获取tuple信息
可用以下方法获取tuple元素个数
tuple拼接tuple> test{ 1,1.23,{"123","456"}}; //获取tuple成员个数 size_t num = tuple_size ::value; //利用tuple某个成员类型来声明变量(以第0个成员为例) tuple_element<0, decltype(test)>::type n = get<0>(test);
tuple交换tupletest(1,2,3); tuple test2(4,5,6); tuple test3(7,8,9); //拼接 auto new_tup = tuple_cat(test, test2, test3);
tupletuple解包test(1,2,3); tuple test2(4,5,6); //交换 test.swap(test2);
tuple使用tie方法进行解包,tie的参数个数与tuple成员个数一致,否则要用ignore占位
tupletuple比较test{2,8}; tuple test2{2,8,6}; int a, b; //解包 tie(a, b) = test; //ignore占位的情况 tie(a, b, ignore) = test2;
当两个tuple具有相同数目的成员,且对应成员类型可比较的时候,tuple比较才合法
//有如下四个tuple tupletuple遍历test{"1","2"}; tuple test2{ 1,2,3 }; tuple test3{1,2}; tuple test4{ 4,5 }; test == test2; //报错,string与int类型不能比较 test2 < test3; //报错,test2与test3成员数量不同 test3 < test4; //正确,该语句值为True
tuple没有迭代器,其遍历非常麻烦,其设计目的也不在于此(如果项目开发过程中要对某一数据结构进行遍历该数据结构应尽量避免为tuple类型,完全可以用list代替)
但此处还是给出遍历的方法(不推荐使用)
本方法来自C++Tuple元组的详细用法 - 知乎 (zhihu.com)
#includetuple开发时的应用#include #include #include using namespace std; template struct PrintTuple { static void Printf(const Tuple& Value) { PrintTuple ::Printf(Value); cout << "," << get (Value); } }; template struct PrintTuple { static void Printf(const Tuple& Value) { cout << get<0>(Value); } }; template void PrintfMyTuple(const tuple & vlaue) { PrintTuple ::Printf(vlaue); } int main() { tuple a(2, 3, 1, 4); PrintfMyTuple(a); system("pause"); return 0; }
在项目开发时,如果我们想让一个函数返回多个不同类型的值的话可以使用tuple。
至于其他情况有待探索。



