我有以下类型:
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;
我需要pairs_vec_t
使用来自 的值初始化 的实例X_vec_t
。我使用以下代码,它按预期工作:
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;
}
我想要做的是使用 using 将我的复制块减少到一行boost::bind
。此代码不起作用:
for_each( xs.begin(), xs.end(), boost::bind( &pairs_vec_t::push_back, ps, boost::bind( &std::make_pair, _1, X2() ) ) );
我知道它为什么不工作,但我想知道如何在不声明额外函数和结构的情况下使其工作?