C++ 元组

// 创建元组的方法
// Packing values into tuple
auto first = make_tuple(10, 'A');
const int maxN = 1e9;
const int maxL = 15;
auto second = make_tuple(maxN, maxL);
cout << get<0>(first) << " " << get<1>(first) << "\n";
cout << get<0>(second) << " " << get<1>(second) << "\n";
//10 A
//1000000000 15

// Unpacking tuple into variables
int first_int;
char first_char;
tie(first_int, first_char) = first;
cout << first_int << " " << first_char << "\n"; //10 A

tuple<int, char, double> third(11, 'A', 3.14141);
cout << tuple_size<decltype(third)>::value << "\n"; //3

//拼接元组
auto concatenated_tuple = tuple_cat(first, second, third);
// concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A', 3.14141)

cout << get<0>(concatenated_tuple) << "\n"; //  10
cout << get<3>(concatenated_tuple) << "\n"; //  15
cout << get<5>(concatenated_tuple) << "\n"; //  'A'

参考:
learn C++ in y minutes

猜你喜欢

转载自blog.csdn.net/m0_37586991/article/details/88911360
今日推荐