I play with functors in C ++. In particular, I have a vector of pairs that I would like to sort by the first element of the pair. I started writing a fully specialized functor (for example, "bool MyLessThan (MyPair & lhs, MyPair & rhs)"). Then, simply because this material is interesting, I wanted to try writing a common functor, "Apply F to the first elements of this pair." I wrote below, but g ++ doesn't like it. I get:
error: type / value mismatch in argument 2 in the list of template parameters for 'template struct Pair1stFunc2' error: expected type, received "less"
#include <algorithm>
#include <functional>
#include <utility>
#include <vector>
template <class P, class F>
struct Pair1stFunc2
{
typename F::result_type operator()(P &lhs, P &rhs) const
{ return F(lhs.first, rhs.first); }
typename F::result_type operator()(const P &lhs, const P &rhs) const
{ return F(lhs.first, rhs.first); }
};
typedef std::pair<int,int> MyPair;
typedef std::vector<MyPair> MyPairList;
MyPairList pairs;
void foo(void)
{
std::sort(pairs.begin(),
pairs.end(),
Pair1stFunc2<MyPair, std::less>());
}
- , ? , , , , STL-fu.