如何使用 BOOST_FOREACH 同时迭代两个向量?
我想用 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 );
}
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 );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
同时迭代两个事物称为“zip”(来自函数式编程),并且 Boost 有一个 zip 迭代器:
请注意,它是一个迭代器,而不是范围,因此要使用
BOOST_FOREACH
,您必须将其中两个填充到 iterator_range 或pair
。所以它不会很漂亮,但是稍微小心一点,您可能可以想出一个简单的 zip_range 并编写:或者 2 的特殊情况并使用 std::pair code> 而不是 boost::tuple。
我想,由于
doSomething
可能有参数(int&, int&)
,实际上我们想要一个tuple
。希望它有效。Iterating over two things simultaneously is called a "zip" (from functional programming), and Boost has a zip iterator:
Note that it's an iterator, not a range, so to use
BOOST_FOREACH
you're going to have to stuff two of them into an iterator_range orpair
. So it won't be pretty, but with a bit of care you can probably come up with a simplezip_range
and write:Or special-case for 2 and use
std::pair
rather thanboost::tuple
.I suppose that since
doSomething
might have parameters(int&, int&)
, actually we want atuple<int&,int&>
. Hope it works.如果你使用 boost,我认为它应该像这样简单:
奇怪的部分是 boost::combine 没有记录。无论如何,对我有用。
If you use boost, I think it should be as simple as:
the strange part is that boost::combine is not documented. Works for me, anyway.
如果您想使用
BOOST_FOREACH
同时迭代两个向量,就像您在示例代码中所做的那样,那么您必须将这两个向量封装在一个包装类中,该类应该公开begin 和
end
函数。这些函数返回自定义迭代器,用于迭代包装器,包装器内部将迭代两个向量。听起来不太好,但这就是你必须做的。这是我第一次尝试实现这个(最小实现只是为了演示基本思想):
以下是测试代码。因为它使用通常的
for
循环,因为 ideone 尚未安装 for boost for C++0x 或者我在包含它时做错了什么。输出:
演示: http://ideone.com/Hf667
这仅适用于实验和学习目的,因为我没有声明它做到完美。可以有很多改进。 @Steve 已经发布了 boost 的解决方案。
If you want to use
BOOST_FOREACH
to iterate two vectors simultenously, as you've done in your sample code, then you've to encapsulate both vectors in a wrapper class which should exposebegin
andend
functions. These functions return custom iterator to be used to iterate over the wrapper which internally will iterate over the two vectors. Doesn't sound good, but that is what you've to do.This is my first attempt to implement this (minimal implementation just to demonstrate the basic idea):
And the following is the test code. Since it's using usual
for
loop, because ideone has not installed for boost for C++0x or I'm doing something wrong when including it.Output:
Demo : http://ideone.com/Hf667
This is good for experimentation and learning purpose only, as I don't claim it to be perfect. There can be lots of improvement. And @Steve already has posted boost's solution.
感谢 Steve Jessop 的回答和精彩评论,我提出了以下解决方案,所以如果您觉得不错,请先投票给 Steve Jessop 的答案。 ;)
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. ;)