user3250551
user3250551

Reputation: 25

Setting the Values of Tuple types in C++

Suppose I have some tuple:

tuple<int, float>

How would I set the value of the int and float, respectively. Like:

int = 4, float = 3.45?

Upvotes: 0

Views: 1819

Answers (1)

nosid
nosid

Reputation: 50074

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);

Since C++14, you can also index tuples by type, if the type is unique within the tuple. That means, you can write code as follows:

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

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

Upvotes: 3

Related Questions