[Boost-users] Foreach For iterating two containers

53 views
Skip to first unread message

Gokulakannan Somasundaram

unread,
Feb 21, 2010, 9:50:38 AM2/21/10
to boost...@lists.boost.org
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.

Gokulakannan Somasundaram

unread,
Feb 21, 2010, 9:54:06 AM2/21/10
to boost...@lists.boost.org
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

Gokulakannan Somasundaram

unread,
Feb 21, 2010, 1:20:11 PM2/21/10
to boost...@lists.boost.org
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.

Pete Bartlett

unread,
Feb 21, 2010, 3:45:52 PM2/21/10
to boost...@lists.boost.org
Gokulakannan Somasundaram wrote:

>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());
\

[...]

If you don't mind the "weight" of BOOST_TYPEOF just for a for loop, it's not
bad as some of the complexity is hidden in that inner macro. However bear in
mind in falls short of the original FOREACH because it uses its arguments r1
and r2 repeatedly which may be inefficient, or even flat out wrong, if r1
and/or r2 are temporaries.

Also, there is an open ticket against some extra functionality in
Boost.Foreach - BOOST_FOREACH_FIELD I think it is called. If that macro came
to pass, I think you could solve your use case with a make_zip_range.

Pete


_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Reply all
Reply to author
Forward
0 new messages