I have the 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 to initialize instance of pairs_vec_t
with values from X_vec_t
. I use the following code and it works as expected:
int main()
{
pairs_vec_t ps;
X_vec_t xs; // this is not empty in the production code
ps.reserve( xs.size() );
{ // I want to change this block to one 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 to do is to reduce my copying block to one line with using boost::bind
. This code is 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 it is not working, but I want to know how to make it working without declaring extra functions and structs?