views:

109

answers:

3

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?

+4  A: 

something like this?

using boost::lambda;
X2 x;
transform(..., (bind(std::make_pair<X,X2>, _1, ref(x))));

I cant check at the moment, but if recalled correctly from memory, the above is valid.

aaa
Thanks, completly forgot about that `make_pair` is a template function.
Kirill V. Lyadvinsky
@kirill long evening? :-)
aaa
+3  A: 
std::for_each( xs.begin(), xs.end(),
               boost::bind( &pairs_vec_t::push_back, &ps,
// you need to pass a pointer —— at least for gcc.   ^ 
                            boost::bind( &std::make_pair<X,X2>, _1, X2() ) ) );
// you need to specify which make_pair to instantiate   ^^^^^^ 
KennyTM
Thanks, completly forgot about that `make_pair` is a template function.
Kirill V. Lyadvinsky
A: 

No Boost needed. std::bind2nd is being deprecated in favor of std::bind adopted from Boost, but for now it's the standard.

pairs_vec_t ps( xs.size() );
transform( xs.begin(), xs.end(), ps.begin(),
    bind2nd( ptr_fun( make_pair<X,X2> ), X2() ) );

If you do have Boost, it's more efficient to insert a range (of transform_iterator) than iterate on push_back or pre-size:

pairs_vec_t ps(
    make_transform_iterator( xs.begin(), bind2nd( ptr_fun( make_pair<X,X2> ), X2() ) ),
    make_transform_iterator( xs.end(), bind2nd( ptr_fun( make_pair<X,X2> ), X2() ) ) );

How's that for a one-liner?

Potatoswatter