Question

I have two containers, and I need to iterate over them both at the same time, in the same loop. I'm using boost foreach.

Like so:

  #define foreach_ BOOST_FOREACH

  struct Vertex
  {
     int x;
     int y;
  }

  std::deque<Vertex>::iterator target_it = targets.begin();

  // Put items in correct position 
  foreach_(Vertex v, coords) 
  {
    v.y = (*target_it).y;
    target_it++;
  }

Where coords is a std::deque .

Can I also put targets into that foreach somehow? The doc suggests no.

Was it helpful?

Solution

You probably want to use a Boost Zip Iterator to traverse the two in parallel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top