【发布时间】:2014-04-18 16:11:46
【问题描述】:
我发现 binary_function 已从 C++11 中删除。我想知道为什么。
C++98:
template <class T> struct less : binary_function <T,T,bool> {
bool operator() (const T& x, const T& y) const {return x<y;}
};
C++11:
template <class T> struct less {
bool operator() (const T& x, const T& y) const {return x<y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
修改--------------------------------------- --------------------------------------
template<class arg,class result>
struct unary_function
{
typedef arg argument_type;
typedef result result_type;
};
例如,如果我们想在 C++98 中为函数编写适配器,
template <class T> struct even : unary_function <T,bool> {
bool operator() (const T& x) const {return 0==x%2;}
};
find_if(bgn,end,even<int>()); //find even number
//adapter
template<typename adaptableFunction >
class unary_negate
{
private:
adaptableFunction fun_;
public:
typedef adaptableFunction::argument_type argument_type;
typedef adaptableFunction::result_type result_type;
unary_negate(const adaptableFunction &f):fun_(f){}
bool operator()(const argument_type&x)
{
return !fun(x);
}
}
find_if(bgn,end, unary_negate< even<int> >(even<int>()) ); //find odd number
如果没有unary_function,我们如何在 C++11 中改进这一点?
【问题讨论】:
标签: c++ c++11 stl functor unary-function