c++ - Copy vector of values to vector of pairs in one line -
i have following types:
struct x { int x; x( int val ) : x(val) {} }; struct x2 { int x2; x2() : x2() {} }; typedef std::pair<x, x2> pair_t; typedef std::vector<pair_t> pairs_vec_t; typedef std::vector<x> x_vec_t;
i need initialize instance of pairs_vec_t
values x_vec_t
. use following code , works expected:
int main() { pairs_vec_t ps; x_vec_t xs; // not empty in production code ps.reserve( xs.size() ); { // want change block 1 line code. struct get_pair { pair_t operator()( const x& value ) { return std::make_pair( value, x2() ); } }; std::transform( xs.begin(), xs.end(), back_inserter(ps), get_pair() ); } return 0; }
what i'm trying reduce copying block 1 line using boost::bind
. code not working:
for_each( xs.begin(), xs.end(), boost::bind( &pairs_vec_t::push_back, ps, boost::bind( &std::make_pair, _1, x2() ) ) );
i know why not working, want know how make working without declaring functions , structs?
something this?
using boost::lambda; x2 x; transform(..., (bind(std::make_pair<x,x2>, _1, ref(x))));
i cant check @ moment, if recalled correctly memory, above valid.
Comments
Post a Comment