반복자를 사용하여 두 개의 컬렉션을 반복하고, 다른 컬렉션과 관련된 (충분히 복잡한) 알고리즘을 기반으로 반복 컬렉션을 수정하고 싶습니다.하나 이상의 컬렉션을 반복하면서 반복자에 쓰기.
#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;
}
가 어떻게이 ???
의 사이 부분을 선언 (또는 기타 루프 3의 매개 변수 변경) 할 : 다음과 같은 최소한의 예를 고려?
- 특히, 당신은 당신이 정말로 무슨 말을 하려는지 설명하기 때문에 거의 완료했습니다 세 번째 루프를 완성하려면 지금 당장은 견고한 답을 제공하는 것이 불가능합니다. 나는 대답을했지만 WRT는 세 번째 루프를 WRT 한 것입니다. 정말로 원한다. –