boost::zip_iteratorを使ってみる
2つのイテレータを並列に進めて2倍した値をv2に格納する
#include <iostream> #include <vector> #include <numeric> #include <boost/iterator/zip_iterator.hpp> int main() { std::vector< int > v( 5 ); std::vector< int > v2( 5 ); std::iota( v.begin(), v.end(), 1 ); auto it = boost::make_zip_iterator( boost::make_tuple( v.begin(), v2.begin() ) ); auto it2 = boost::make_zip_iterator( boost::make_tuple( v.end(), v2.end() ) ); std::for_each( it, it2, []( auto a ) { a.get<1>() = a.get<0>() * 2; //***iteratorのコピーaから参照にアクセスしているので,値が書き換わる*** } ); for( auto const & i : v2 ) { std::cout << i << std::endl; } /*output* 2 4 6 8 10 ********/ return 0; }
writableじゃないのでstd::sortは使えないなどの問題はあるんだけれど,まぁ,それなりには使えそう.