我想使用迭代器遍历两个集合,根据涉及另一个的(足够复杂的)算法修改一个。考虑以下最小示例:
#include <iostream>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/range/combine.hpp>
#include <boost/tuple/tuple.hpp> // tie
using namespace std;
using namespace boost;
int main(void) {
// input data; never mind how these get filled
int aa[] = {2, 3, 5, 8, 13, 21};
int bb[] = {1, 0, 1, 1, 0, 1};
vector<int> a (&aa[0], &aa[sizeof(aa)/sizeof(aa[0])]);
vector<int> b (&bb[0], &bb[sizeof(bb)/sizeof(bb[0])]);
// output storage; assume it has always correct dim.
vector<int> c (a.size());
// iterate through two coll., reading from both
int p, q;
BOOST_FOREACH (tie(p,q), combine(a,b)) { // loop1
cout << p << "*" << q << "=" << p*q << endl;
}
// iterate through one coll., writing to it
BOOST_FOREACH (int& r, c) { // loop2
r = 42;
}
// iterate through two coll., reading from one, writing to the other?
BOOST_FOREACH (??? p, s ???, combine(a,c)) { // loop3
s = p * 2;
}
return 0;
}
如何声明???
s 之间的部分(或以其他方式更改 loop3 中的参数)?