Boost logo

Boost Users :

Subject: Re: [Boost-users] Foreach For iterating two containers
From: Gokulakannan Somasundaram (gokul007_at_[hidden])
Date: 2010-02-21 13:20:11


Meanwhile, a gentleman helped me by providing this code. Posting it here for
any comments

#include <boost/typeof/typeof.hpp>

#define FOREACH_2(def1, def2, r1, r2)
         \
    for(std::pair<BOOST_TYPEOF((r1).begin()),
         \
                  BOOST_TYPEOF((r2).begin())>
_it12_((r1).begin(),(r2).begin()),\
                                            _e12_((r1).end(),
(r2).end()); \
            _it12_.first != _e12_.first && _it12_.second !=
_e12_.second; \
            ++_it12_.first, ++_it12_.second)
         \
        if(int _cnt1_ = 1)
         \
            for(def1 = *_it12_.first; _cnt1_; _cnt1_ = 0)
         \
                if(int _cnt2_ = 1)
         \
                    for(def2 = *_it12_.second; _cnt2_; _cnt2_ = 0)

Thanks,
Gokul.

On Sun, Feb 21, 2010 at 8:24 PM, Gokulakannan Somasundaram <
gokul007_at_[hidden]> wrote:

> Sorry the construct is like this
>
>
> std::vector<int>::iterator iter1;
> std::vector<std::string>::iterator iter2;
> for( iter1 = cont1->begin(), iter2 = cont2->begin();
> iter1 != cont1->end() *&&* iter2 != cont2->end(); ++iter1,
> ++iter2 )
> {
> int x = *iter1;
> std::string y = *iter2;
>
> .....
> }
>
> I have change the || into &&.
>
> Thanks,
> Gokul
>
>
> On Sun, Feb 21, 2010 at 8:20 PM, Gokulakannan Somasundaram <
> gokul007_at_[hidden]> wrote:
>
>> Hi,
>> Is it possible to use Boost Foreach to iterate over two containers
>> simultaneously? I have done my preliminary search in the lists and i
>> couldn't find a solution. I am asking for a solution to iteration like this
>>
>> std::vector<int>::iterator iter1;
>> std::vector<std::string>::iterator iter2;
>>
>> for( iter1 = cont1->begin(), iter2 = cont2->begin();
>> iter1 != cont1->end() || iter2 != cont2->end(); ++iter1,
>> ++iter2 )
>> {
>> int x = *iter1;
>> std::string y = *iter2;
>>
>> .....
>> }
>>
>> If this is not present, please consider this as a feature request. The
>> syntax below can also be considered
>>
>> forboth( int x, std::string y, cont1, cont2 )
>> {
>> }
>>
>>
>> Thanks,
>> Gokul.
>>
>
>



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