How to set std :: tuple element by index?

You can get an element from std::tuple by index using std::get . Similarly, how to set the tuple element by index?

+45
c ++ indexing templates tuples
Sep 17 '11 at 8:45
source share
2 answers

std::get returns a reference to the value. Therefore, you set the value as follows:

 std::get<0>(myTuple) = newValue; 

This, of course, assumes that myTuple not const. You can even move elements from the tuple via std::move by calling it in the tuple:

 auto movedTo = std::get<0>(std::move(myTuple)); 
+63
Sep 17 '11 at 8:50
source share

The non-constant version of get returns a link . You can assign a link. For example, suppose t is a tuple, then: get<0>(t) = 3;

+13
Sep 17 2018-11-11T00:
source share



All Articles