Convert two vectors into a vector of tuples using stl and boost

262 Views Asked by At

i have

vector<time> tt;
vector<space> xx;

i would like to get

vector<tuple<time,space>> trajectory;

this is what I have so far

  std::for_each(boost::make_zip_iterator(
        boost::make_tuple(tt.begin(),xx.begin(),
        boost::make_tuple(tt.end(),xx.end(),
        []() {
          trajectory.push_back(make_tuple(get<0>(),get<1>()));
        }
        );

i'm not sure how to proceed further. one way is i can make a functor as zip_func in http://www.boost.org/doc/libs/1_52_0/libs/iterator/doc/zip_iterator.html#examples but i don't want to write extra code and I want to use lambda. any thoughts?

1

There are 1 best solutions below

0
On

How about something like (untested code):

std::copy( 
    boost::make_zip_iterator(boost::make_tuple(tt.begin(),xx.begin()),
    boost::make_zip_iterator(boost::make_tuple(tt.end  (),xx.end  ()),
    std::back_inserter(trajectory)
    );

No need for a lambda! In your sample code you are unpacking a tuple only to create another tuple, so I was able to get rid of that.