tupe元组是一个固定大小的不同类型值的集合,是泛化的std::pair,可以把它当结构体使用。
构造一个元组:
tuple<const char*, int> tp = make_tuple("sendpack", 8);
或
int i = 1;
auto tp = std::tie("aa", i);
相当于:
struct tp
{
char* p;
int len;
};
获取元组的值,使用std::get获取某个值:
const char* data = std::get<0>(tp);
使用std::tie解包:
const char* a;
int x;
std::tie(a, x) = tp;
如果不想解某个位置的值可以使用std::ignore,如std::tie(std::ignore, x) = tp;
使用tuple_cat可以连接多个元组:
tuple<int, string, double> t1(10, "test", 2.1);
int n = 7;
auto t2 = tuple_cat(t1, make_tuple("Foo", "bar"), t1, std::tie(n));
n = 10;
print(t2);
// 10 test 2.1 Foo bar 10 test 2.1 10