c++boost-foreach

Is it possible to iterate over multiple containers with boost foreach?


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.


Solution

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