문제

boost range transform requires const & for ranges in arguments.

#include <iostream>
#include <vector>
#include <boost/range/algorithm.hpp>

int main(int argc, char *argv[])
{
  using namespace std;

  vector<vector<int>> rt0(10,vector<int>(15,2));
  vector<vector<int>> irt(10,vector<int>(15,5));

  for(auto & i:rt0) {
    for(auto& j:i) cout << j << "  ";
    cout << "\n";
  }
  cout << "\n";
  for(auto & i:irt) {
    for(auto& j:i) cout << j << "  ";
    cout << "\n";
  }

  boost::transform(rt0,irt,rt0.begin(),
    [] (const vector<int> &t0,const vector<int> &it) {
      auto tt = t0;
      boost::transform(t0,it,tt.begin(), plus<int>());
      return tt;
    }
  );
  cout << "\n";
  cout << "\n";
  for(auto & i:rt0) {
    for(auto& j:i) cout << j << "  ";
    cout << "\n";
  }
  return 0;
}

compile and run with

g++ -std=c++11 main.cc; ./a.out

if boost::transform's BinaryOperation took & instead of const & for SinglePassRange1 then i wouldn't have needed to create a copy (auto tt = t0) and just use to instead of tt. is there a way to avoid creating this copying (while still using ranges)?

link to boost range transform: http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/range/reference/algorithms/mutating/transform.html

도움이 되었습니까?

해결책

I need to use for_each either with a tuple or the new boost implementation that takes two arguments.

conceptually transform should not modify. it comes from functional programming where there is no in-place modification.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top