Changing a tuple in a C ++ tuple vector

I have a tuple vector<tuple<int,int>> vector; , and I want to change one of its tuples.

 for (std::tuple<int, int> tup : std::vector) { if (get<0>(tup) == k) { /* change get<1>(tup) to a new value * and have that change shown in the vector */ } } 

I am not sure how to change the value of the tuple and be reflected in the vector. I tried using

 get<1>(tup) = v; 

but this does not change the meaning of the tuple in the vector. How can i do this? Thanks.

+6
source share
2 answers

Grab tuple at the link:

 for (tuple<int, int> &tup : vector){ // ^here if (get<0>(tup) == k){ get<1>(tup) = v; } } 
+14
source

You just need to use the link instead of the value in your for-loop:

 for (tuple<int, int>& tup : vector){ 
+5
source

Source: https://habr.com/ru/post/983362/


All Articles