Trying to use boost lambda but my code won't compile
I am trying to use boost lambda to avoid having to write trivial functors. For example, I want to use a lambda to access a member of a structure or call a method of a class, like this:
#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 );
When I try to compile this, I get the following errors:
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>
]
Since the vp contains pair<int,int>
I thought _1.the first one should work. What am I doing wrong?
a source to share
What you want is something similar to:
#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) );
a source to share
According to this , I believe the syntax is
sort(vp.begin(), vp.end(),
bind(&pair<int,int>::first, _1) > bind(&pair<int,int>::first, _2));
However, I would like to point out (not so?) The obvious - sort(vp.begin(), vp.end());
will basically do the same. pair
by default are sorted by their first argument and then by their second. Since you are using sort
(which is unstable) you will have all the pairs sorted by first
, and then the pairs with equal first
will be in more or less random order.
If you want to preserve the order when the first elements are equal, you should use instead stable_sort
.
a source to share