ham*_*mcn 2 c++ compiler-errors boost-lambda
我正在尝试使用boost lambda来避免编写琐碎的仿函数.例如,我想使用lambda来访问结构的成员或调用类的方法,例如:
#include <vector>
#include <utility>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
using namespace std;
using namespace boost::lambda;
vector< pair<int,int> > vp;
vp.push_back( make_pair<int,int>(1,1) );
vp.push_back( make_pair<int,int>(3,2) );
vp.push_back( make_pair<int,int>(2,3) );
sort(vp.begin(), vp.end(), _1.first > _2.first );
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我得到以下错误:
error C2039: 'first' : is not a member of 'boost::lambda::lambda_functor<T>'
with
[
T=boost::lambda::placeholder<1>
]
error C2039: 'first' : is not a member of 'boost::lambda::lambda_functor<T>'
with
[
T=boost::lambda::placeholder<2>
]
Run Code Online (Sandbox Code Playgroud)
由于vp包含pair<int,int>
我认为_1.first应该工作.我做错了什么?
你想要的是类似于:
#include <boost/lambda/bind.hpp> // new header
// typedefs make code easier
typedef pair<int,int> pair_type;
typedef vector<pair_type> vector_type;
vector_type vp;
vp.push_back( make_pair(1,1) ); // don't specify template arguments!
vp.push_back( make_pair(3,2) ); // the entire point of make_pair is
vp.push_back( make_pair(2,3) ); // to deduce them.
sort(vp.begin(), vp.end(),
bind(&pair_type::first, _1) > bind(&pair_type::first, _2) );
Run Code Online (Sandbox Code Playgroud)