Kir*_*sky 1 c++ boost stl boost-bind
我有以下类型:
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;
Run Code Online (Sandbox Code Playgroud)
我需要初始化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;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试做的是使用使用将我的复制块减少到一行boost::bind.此代码无效:
for_each( xs.begin(), xs.end(), boost::bind( &pairs_vec_t::push_back, ps, boost::bind( &std::make_pair, _1, X2() ) ) );
Run Code Online (Sandbox Code Playgroud)
我知道为什么它不起作用,但我想知道如何使它工作而不声明额外的功能和结构?
这样的事情?
using boost::lambda;
X2 x;
transform(..., (bind(std::make_pair<X,X2>, _1, ref(x))));
Run Code Online (Sandbox Code Playgroud)
我暂时无法检查,但如果从内存中正确调用,则上述内容有效.
| 归档时间: |
|
| 查看次数: |
474 次 |
| 最近记录: |