Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to iterate over a vector of pair of pair

Tags:

c++

auto

Is there an easy way to iterate over a vector of pair of pair using auto?

I have a vector<pair<pair<int,int>, int>> vec and want to iterate something like.

for(auto [x, y,z] : vec)

but I am getting an error. Is there an easy way to do so?

for(auto [[x,y],z] : vec)

also gives an error.

like image 452
White Knife Avatar asked Dec 31 '22 20:12

White Knife


2 Answers

You can try something like shown below.

for (auto& it: vec) {
  auto[x, y, z] = tie(it.first.first, it.first.second, it.second);
}
like image 177
Rishabh Deep Singh Avatar answered Jan 12 '23 20:01

Rishabh Deep Singh


You could write:

for (auto & [p, z] : vec) 
{
  auto & [x, y] = p;
  // ... use x, y, z
}
like image 28
cigien Avatar answered Jan 12 '23 22:01

cigien