函数对象:函数对象就是重载类中了operator()运算符的类类型对象,当使用该类实例调用operator函数时如同调用普通函数一样。从概念上来说,函数对象是用作函数的对象,但从实现来说,函数对象是实现了()函数的对象。虽然函数和函数指针也是函数对象,但只有实现了operator()函数的类对象才能保存类成员状态。
函数对象的优势有以下几个:
1)函数对象可以有自己的状态。当在类中定义状态变量时,多次调用函数对象可以共享这个状态变量。普通的函数调用没有这个优势。
2)函数对象有自己特有的类型,而普通函数没有。这样在使用STL模板时,函数对象可以定义自己的规则。
// ConsoleApplication42.cpp : 定义控制台应用程序的入口点。
//函数对象
#include"stdafx.h"
#include<string>
#include<iostream>
#include<vector>
#include<queue>
#include <functional>
using namespace std;
//template<class _Arg1,
//class _Arg2,
//class _result>
//class binary_function{
//public:
// typedef _Arg1 first_argument;
// typedef _Arg2 second_argument;
// typedef _result result_argument;
//};
class com {
public:
bool operator()(int& p1,
int& p2){
return p1 > p2;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
priority_queue<int, vector<int>, com> nums;
int num;
cout << "input numbers 0 to end" << endl;
while (1)
{
cin >> num;
if (0 == num)
{
break;
}
nums.push(num);
}
while (!(nums.empty()))
{
cout << nums.top() << " ";
nums.pop();
}
system("pause");
return 0;
}
STL标准库中,prioprity queue使用降序来对队列中的元素排列,在这里我们定义了函数对象muns,nums按照升序对队列中的元素排列。
// ConsoleApplication42.cpp : 定义控制台应用程序的入口点。
//函数对象
#include"stdafx.h"
#include<string>
#include<iostream>
#include<vector>
#include<queue>
#include <functional>
using namespace std;
class SuccessiveNumGen
{
public:
SuccessiveNumGen(int origin = 0) :<span style="font-family: Arial, Helvetica, sans-serif;">m_origin</span><span style="font-family: Arial, Helvetica, sans-serif;">(origin){}</span>
int operator() ()
{
return m_origin++;
}
private:
int m_origin;
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> dest;
generate_n(back_inserter(dest), 10, SuccessiveNumGen(3));
vector<int>::iterator iter;
for (iter = dest.begin(); iter != dest.end(); iter++){
cout << *iter<< endl;
}
system("pause");
return 0;
}
SuccessiveNumGen函数被连续调用10次,m_origin状态被保存,生成序列3,4,5, ,12.
函数适配器
除了自定义的函数对象,标准库还为我们提供了一系列现成的函数对象, 比如常见的数学、逻辑运算等。例如:negate<type>(),plus<type>(),minus<type>(),multiplies<type>(),divides<type>(),modulus<type>(),equal_to<type>,greater<type>(),less<type>(),logical_not<type>(),logical_and<type>(),等等。
函数式配器本质上也是一种函数对象,通过将一个或多个函数对象或特定数据按一定规则组合起来,完成特定功能。STL为我们提供了几种适配器,其中bind1st和bind2nd两个包装函数即可返回适配器。这两个函数均有两个形参,分别为二元函数对象和一个数值,其中bind1st表示将数值绑定到二元函数的第一个形参,bind2nd表示将数值绑定到二元函数的第二个形参,并返回一个一元对象。
这两个函数函数模板如下:
template<typename Operator,typename T>
binder1st bind1st(const Operator &op, const T &t){
return binder1st<Operator>(op, typename Operator::first_argument_type(t));
}
template<typename Operator,typename T>
binder2nd bind2nd(const Operator &op, const T &t){
return binder2nd<Operator>(op, typename Operator::second_argument_type(t));
}
template<class Operation>
class binder1st : public unary_function<typename Operation::second_argument_type, typename Operation::return_type>
{
public:
binder1st(const Operation &op, const typename Operation::first_argument_type &arg) : operation(op), arg_1st(arg) {}
typename Operation::return_type operator() (const typename Operation::second_argument_type &arg_2nd) const
{
return operation(arg_1st, arg_2nd);
}
private:
Operation operation;
typename Operation::first_argument_type arg_1st;
};
// ConsoleApplication42.cpp : 定义控制台应用程序的入口点。
//函数对象
#include"stdafx.h"
#include<string>
#include<iostream>
#include<vector>
#include<queue>
#include <functional>
using namespace std;
//template <class Arg1, class Arg2, class Result>
//struct binary_function
//{
// typedef Arg1 first_argument_type;
// typedef Arg2 second_argument_type;
// typedef Result result_type;
//};
template<typename T1, typename T2>
class Pow : public binary_function<T1, T2, T1>
{
public:
T1 operator() (T1& base, T2 exp) const
{
return std::pow(base, exp);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<float> vec;
for (int i = 1; i < 3; i++){
vec.push_back(i+1);
}
vector<float>::iterator iter=find_if(vec.begin(), vec.end(), bind2nd(Pow<float, int>(), 3));
cout <<*iter << endl;
for (iter = vec.begin(); iter != vec.end(); iter++){
cout <<*iter << endl;
}
system("pause");
return 0;
}