在C++中设置元组类型的值



假设我有一个元组:

tuple<int, float>

如何分别设置int和float的值。类似:

int=4,float=3.45?

std::tuple<int, float> t;
// set int to 4
std::get<0>(t) = 4;
// set float to 3.45
std::get<1>(t) = 3.45;
// set both values
t = std::make_tuple(4, 3.45);

由于C++14,如果元组中的类型是唯一的,那么也可以按类型为元组编制索引。这意味着,您可以按照以下方式编写代码:

// set int to 4
std::get<int>(t) = 4;
// set float to 3.45
std::get<float>(t) = 3.45;

最新更新