34

I'd like to replicate the following with BOOST FOREACH

std::vector<int>::const_iterator i1;
std::vector<int>::const_iterator i2;
for( i1 = v1.begin(), i2 = v2.begin();
     i1 < v1.end() && i2 < v2.end();
     ++i1, ++i2 )
{
     doSomething( *i1, *i2 );
}
4

4 回答 4

35

同时迭代两件事被称为“zip”(来自函数式编程),Boost 有一个 zip 迭代器

zip 迭代器提供了同时对多个受控序列进行并行迭代的能力。zip 迭代器由迭代器元组构成。移动 zip 迭代器并行移动所有迭代器。取消引用 zip 迭代器会返回一个元组,其中包含取消引用各个迭代器的结果。

请注意,它是一个迭代器,而不是一个范围,因此要使用BOOST_FOREACH您必须将其中两个填充到iterator_rangepair. 所以它不会很漂亮,但稍加注意,你可能会想出一个简单的zip_range写法:

BOOST_FOREACH(boost::tuple<int,int> &p, zip_range(v1, v2)) {
    doSomething(p.get<0>(), p.get<1>());
}

或 2 的特殊情况并使用std::pair而不是boost::tuple.

我想既然doSomething可能有参数(int&, int&),实际上我们想要一个tuple<int&,int&>. 希望它有效。

于 2011-09-02T17:22:58.220 回答
16

如果你使用boost,我认为它应该很简单:

#include <boost/foreach.hpp>
#include <boost/range/combine.hpp>
std::vector<int> v1;
std::vector<int> v2;

// iterate over values
int i1, i2;
BOOST_FOREACH(boost::tie(i1, i2), boost::combine(v1, v2))
    std::cout << i1+i2 << "\n"; // sums two vectors

// iterate over references
typedef boost::tuple<int&, int&> int_ref_tuple;
BOOST_FOREACH(int_ref_tuple tup, boost::combine(v1, v2))
    tup.get<0>() = tup.get<1>(); // assigns one vector to another

奇怪的是 boost::combine 没有记录。无论如何,对我有用。

于 2012-11-27T09:33:51.423 回答
8

如果您想同时BOOST_FOREACH迭代两个向量,就像您在示例代码中所做的那样,那么您必须将两个向量封装在一个应该公开beginend函数的包装类中。这些函数返回自定义迭代器以用于迭代包装器,该包装器在内部将迭代两个向量。听起来不太好,但这是你必须做的。

这是我第一次尝试实现这个(最小实现只是为了展示基本思想):

template<typename T>
struct wrapper
{
    struct iterator
    {
         typedef typename std::vector<T>::iterator It;
         It it1, it2;
         iterator(It it1, It it2) : it1(it1), it2(it2) {}
         iterator & operator++()
         {
            ++it1; ++it2; return *this;
         }
         iterator & operator *()
         {
            return *this;
         }
         bool operator == (const iterator &other)
         {
             return !(*this != other);
         }
         bool operator != (const iterator &other)
         {
             return it1 != other.it1 && it2 != other.it2;
         }
    };
    iterator begin_, end_;
    wrapper(std::vector<T> &v1,  std::vector<T> &v2) 
      : begin_(v1.begin(), v2.begin()),end_(v1.end(), v2.end())
    {
    }
    wrapper(const wrapper & other) : begin_(other.begin_), end_(other.end_) {}
    iterator begin() 
    {
          return begin_;
    }
    iterator end() 
    {
          return end_;
    }    
};

以下是测试代码。因为它使用通常的for循环,因为 ideone 没有为 C++0x 安装 boost 或者我在包含它时做错了什么。

int main() {
        std::vector<int> v1 = {1,2,3,4,5,6};
        std::vector<int> v2 = {11,12,13,14,15};
        wrapper<int> w(v1,v2);
        for(wrapper<int>::iterator it = w.begin(); it != w.end(); ++it)
        {
             std::cout << *it.it1 <<", "<< *it.it2 << std::endl;
        }
        return 0;
}

输出:

1, 11
2, 12
3, 13
4, 14
5, 15

演示:http: //ideone.com/Hf667

这仅适用于实验和学习目的,因为我并不认为它是完美的。可以有很多改进。@Steve 已经发布了 boost 的解决方案。

于 2011-09-02T17:07:57.667 回答
4

Thanks to the answer of Steve Jessop and the great comments, I came up to the following solution, so if you find that nice, vote up Steve Jessop answer first. ;)

#include <iostream>
#include <vector>

#include <boost/typeof/typeof.hpp>
#include <boost/typeof/std/vector.hpp>

#include <boost/foreach.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/iterator/zip_iterator.hpp>
#include <boost/range/iterator_range.hpp>

using namespace boost;

int main(int argc, char **argv) {
    std::vector<int> vecFirst = assign::list_of(1)(2)(3)(43)(7)(13);
    std::vector<double> vecSecond = assign::list_of(53.45)(-23.545)(0.1574)(1.001)(0.0047)(9.7);

    BOOST_AUTO(zipSequence,
       make_iterator_range(
           make_zip_iterator(make_tuple(vecFirst.begin(), vecSecond.begin())), 
           make_zip_iterator(make_tuple(vecFirst.end(), vecSecond.end()))
       )
    );

    BOOST_FOREACH( BOOST_TYPEOF(*zipSequence.begin()) each, zipSequence) {
       std::cout << "First vector value : " << each.get<0>() 
                 << " - Second vector value : " << each.get<1>() 
                 << std::endl;
    }
}
于 2012-06-12T21:57:48.493 回答