Boost logo

Boost Users :

From: Eric Niebler (eric_at_[hidden])
Date: 2007-12-02 15:01:08


chun ping wang wrote:
> hi, is their anyway where i can use boost foreach to iterate through two
> different containers at once.
>
> Like I have two vector of float and i want to iterate through them
> parallell.. (not using indexing.. since it doesn't work with all types).
> Thanks.
>
> Something that is equalivent to python,
> for x,y in xArray, yArray:
> # do some awesome stuff.
> print x, y

Not directly, but you can get what you want with the help of some range
adapters. I haven't tried it, but it looks like the "zipped" adapter
from the Oven library can help here. See
http://p-stade.sourceforge.net/oven/doc/html/oven/utilities.html#oven.utilities.zipped.

Untested:

typedef boost::tuple<float, float> point;
BOOST_FOREACH( point p, boost::make_tuple(xArray, yArray) | zipped )
{
    // do some awesome stuff
    std::cout << p.first << p.second;
}

Good luck!

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net