White Knife
White Knife

Reputation: 87

Easy way to iterate over a vector of pair of pair

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.

Upvotes: 4

Views: 745

Answers (2)

cigien
cigien

Reputation: 60228

You could write:

for (auto & [p, z] : vec) 
{
  auto & [x, y] = p;
  // ... use x, y, z
}

Upvotes: 3

Rishabh Deep Singh
Rishabh Deep Singh

Reputation: 914

You can try something like shown below.

for (auto& it: vec) {
  auto[x, y, z] = tie(it.first.first, it.first.second, it.second);
}

Upvotes: 6

Related Questions