C++中的标准函数对象
2008-12-15 23:50
429 查看
函数对象
函数对象是一种利用句法规则特定设计的类生成的对象,它的作用类似于函数。
在C++中,这是通过在类中定义成员函数operator()实现的,举个例子:
struct myclass {
int operator()(int a) {return a;}
}myobject;
int x = myobject(0);
它们作为断定或者比较功能和标准算法(algorithms)一起使用时将非常有用。
在头文件<functional>中标准库提供了好几种标准的函数对象和一些修改方法以适应它们自己的行为。
基类
unary_function 一元的函数对象基类
template <class Arg, class Result>
struct unary_function {
typedef Arg argument_type;
typedef Result result_type;
};
binary_function 二元的函数对象基类
template <class Arg1, class Arg2, class Result>
struct binary_function {
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
算子类
算术操作符
plus + 加函数对象类
template <class T> struct plus : binary_function <T,T,T> {
T operator() (const T& x, const T& y) const
{return x+y;}
};
minus - 减函数对象类
同理,这里就不一一列举
multiplies * 乘函数对象类
divides / 除函数对象类
modulus % 模函数对象类
negate - 负函数对象类
比较操作符
equal_to == 函数对象类用于相等比较
template <class T> struct equal_to : binary_function <T,T,bool> {
T operator() (const T& x, const T& y) const
{return x==y;}
};
not_equal_to != 函数对象类用于不相等比较
同理,这里就不一一列举
greater > 函数对象类用于大于比较
less < 函数对象类用于小于比较
greater_equal >= 函数对象类用于不小于比较
less_equal <= 函数对象类用于不大于比较
逻辑操作符
logical_and && 逻辑与函数对象类
logical_or || 逻辑或函数对象类
logical_not ! 逻辑非函数对象类
适配器和转换功能
否定器
not1 返回一元函数对象的反面
template <class Predicate>
unary_negate<Predicate> not1 (const Predicate& pred)
{
return unary_negate<Predicate>(pred);
}
not2 返回二元函数对象的方面
template <class Predicate>
binary_negate<Predicate> not2 (const Predicate& pred)
{
return binary_negate<Predicate>(pred);
}
参数绑定器
bind1st 返回绑定了第一个参数的函数对象
template <class Operation, class T>
binder1st<Operation> bind1st (const Operation& op, const T& x)
{
return binder1st<Operation>(op, typename Operation::first_argument_type(x));
}
bind2nd 返回绑定了第二个参数的函数对象
template <class Operation, class T>
binder2nd<Operation> bind2nd (const Operation& op, const T& x)
{
return binder2nd<Operation>(op, typename Operation::second_argument_type(x));
}
有用的类型
unary_negate 生成一元函数对象类的反面
template <class Predicate> class unary_negate
: public unary_function <typename Predicate::argument_type,bool>
{
protected:
Predicate fn;
public:
explicit unary_negate ( const Predicate& pred ) : fn (pred) {}
bool operator() (const typename Predicate::argument_type& x) const
{ return !fn(x); }
};
binary_negate 生成二元函数对象类的反面
template <class Predicate> class binary_negate
: public binary_function <typename Predicate::first_argument_type,
typename Predicate::second_argument_type, bool>
{
protected:
Predicate fn;
public:
explicit binary_negate ( const Predicate& pred ) : fn (pred) {}
bool operator() (const typename Predicate::first_argument_type& x,
const typename Predicate::second_argument_type& y) const
{ return !fn(x,y); }
};
binder1st 绑定了第一个参数的函数对象类
template <class Operation> class binder1st
: public unary_function <typename Operation::second_argument_type,
typename Operation::result_type>
{
protected:
Operation op;
typename Operation::first_argument_type value;
public:
binder1st ( const Operation& x,
const typename Operation::first_argument_type& y) : op (x), value(y) {}
typename Operation::result_type
operator() (const typename Operation::second_argument_type& x) const
{ return op(value,x); }
};
binder2nd 绑定了第二个参数的函数对象类
template <class Operation> class binder2nd
: public unary_function <typename Operation::first_argument_type,
typename Operation::result_type>
{
protected:
Operation op;
typename Operation::second_argument_type value;
public:
binder2nd ( const Operation& x,
const typename Operation::first_argument_type& y) : op (x), value(y) {}
typename Operation::result_type
operator() (const typename Operation::first_argument_type& x) const
{ return op(x,value); }
};
还有一小部分没有翻译(这部分我还没看呢),以后另起补上。
函数对象是一种利用句法规则特定设计的类生成的对象,它的作用类似于函数。
在C++中,这是通过在类中定义成员函数operator()实现的,举个例子:
struct myclass {
int operator()(int a) {return a;}
}myobject;
int x = myobject(0);
它们作为断定或者比较功能和标准算法(algorithms)一起使用时将非常有用。
在头文件<functional>中标准库提供了好几种标准的函数对象和一些修改方法以适应它们自己的行为。
基类
unary_function 一元的函数对象基类
template <class Arg, class Result>
struct unary_function {
typedef Arg argument_type;
typedef Result result_type;
};
binary_function 二元的函数对象基类
template <class Arg1, class Arg2, class Result>
struct binary_function {
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
算子类
算术操作符
plus + 加函数对象类
template <class T> struct plus : binary_function <T,T,T> {
T operator() (const T& x, const T& y) const
{return x+y;}
};
minus - 减函数对象类
同理,这里就不一一列举
multiplies * 乘函数对象类
divides / 除函数对象类
modulus % 模函数对象类
negate - 负函数对象类
比较操作符
equal_to == 函数对象类用于相等比较
template <class T> struct equal_to : binary_function <T,T,bool> {
T operator() (const T& x, const T& y) const
{return x==y;}
};
not_equal_to != 函数对象类用于不相等比较
同理,这里就不一一列举
greater > 函数对象类用于大于比较
less < 函数对象类用于小于比较
greater_equal >= 函数对象类用于不小于比较
less_equal <= 函数对象类用于不大于比较
逻辑操作符
logical_and && 逻辑与函数对象类
logical_or || 逻辑或函数对象类
logical_not ! 逻辑非函数对象类
适配器和转换功能
否定器
not1 返回一元函数对象的反面
template <class Predicate>
unary_negate<Predicate> not1 (const Predicate& pred)
{
return unary_negate<Predicate>(pred);
}
not2 返回二元函数对象的方面
template <class Predicate>
binary_negate<Predicate> not2 (const Predicate& pred)
{
return binary_negate<Predicate>(pred);
}
参数绑定器
bind1st 返回绑定了第一个参数的函数对象
template <class Operation, class T>
binder1st<Operation> bind1st (const Operation& op, const T& x)
{
return binder1st<Operation>(op, typename Operation::first_argument_type(x));
}
bind2nd 返回绑定了第二个参数的函数对象
template <class Operation, class T>
binder2nd<Operation> bind2nd (const Operation& op, const T& x)
{
return binder2nd<Operation>(op, typename Operation::second_argument_type(x));
}
有用的类型
unary_negate 生成一元函数对象类的反面
template <class Predicate> class unary_negate
: public unary_function <typename Predicate::argument_type,bool>
{
protected:
Predicate fn;
public:
explicit unary_negate ( const Predicate& pred ) : fn (pred) {}
bool operator() (const typename Predicate::argument_type& x) const
{ return !fn(x); }
};
binary_negate 生成二元函数对象类的反面
template <class Predicate> class binary_negate
: public binary_function <typename Predicate::first_argument_type,
typename Predicate::second_argument_type, bool>
{
protected:
Predicate fn;
public:
explicit binary_negate ( const Predicate& pred ) : fn (pred) {}
bool operator() (const typename Predicate::first_argument_type& x,
const typename Predicate::second_argument_type& y) const
{ return !fn(x,y); }
};
binder1st 绑定了第一个参数的函数对象类
template <class Operation> class binder1st
: public unary_function <typename Operation::second_argument_type,
typename Operation::result_type>
{
protected:
Operation op;
typename Operation::first_argument_type value;
public:
binder1st ( const Operation& x,
const typename Operation::first_argument_type& y) : op (x), value(y) {}
typename Operation::result_type
operator() (const typename Operation::second_argument_type& x) const
{ return op(value,x); }
};
binder2nd 绑定了第二个参数的函数对象类
template <class Operation> class binder2nd
: public unary_function <typename Operation::first_argument_type,
typename Operation::result_type>
{
protected:
Operation op;
typename Operation::second_argument_type value;
public:
binder2nd ( const Operation& x,
const typename Operation::first_argument_type& y) : op (x), value(y) {}
typename Operation::result_type
operator() (const typename Operation::first_argument_type& x) const
{ return op(x,value); }
};
还有一小部分没有翻译(这部分我还没看呢),以后另起补上。
相关文章推荐
- 简化以下程序,将函数对象 divide_by 转换为一个函数,并将 for 循环替换为用一个标准的 C++ 算法来输出数据
- C++中的标准函数对象
- C++ 预定义函数对象以及函数适配器(一)
- C++ 学习之路(3):向函数传递对象
- C++中类的访问权限针对的是类而不是对象!(为什么类中的函数可以访问对象的私有成员?)
- 为什么c++抛出异常后还能对函数内的局部对象进行析构? http://segmentfault.com/q/1010000002498987
- [C++对象模型][5]堆栈与函数调用
- C/C++函数如何返回struct或class对象
- 标准c++中string类函数介绍
- C++的函数对象优于函数指针地方
- C++中的函数指针和函数对象总结
- C++对象模型之成员函数的指针
- C++中 函数,函数模板,函数对象,函数对象模板 与 回调机制 不得不说的事
- 内联函数背景、例子、与普通函数的区别及要注意的地方 ------新标准c++程序设计
- C++中的const对象和const成员函数
- C++学习笔记--函数对象
- 标准C++ 字符串处理增强函数
- C++ STL/ (11) 函数对象
- C++ 函数对象
- C++对象模型之函数成员(1)