C++学习笔记七之函数对象(仿函数)

一、函数对象概念

函数对象:定义了调用操作符()的类对象。当用该对象调用此操作符时,其表现形式如同普通函数调用一般,因此取名叫函数对象。

class A 

{  

public:  

    int operator() ( int val )  

    {  

        return val > 0 ? val : -val;

    }  

};  

二、函数对象的优点

与普通函数相比,函数对象比函数更加灵活,函数对象的优势:

  • 函数对象可以有自己的状态。我们可以在类中定义状态变量,这样一个函数对象在多次的调用中可以共享这个状态;
  • 函数对象有自己特有的类型。我们可以传递相应的类型作为参数来实例化相应的模板,比如说带参数的函数形参。

三、谓词

1、概念

返回布尔类型的仿函数称为谓词。

2、分类

如果operator接受一个参数叫做一元谓词,接受两个参数叫做二元谓词。

四、STL内建函数对象

1、分类

①算数仿函数;②关系仿函数;③逻辑仿函数。

2、使用

这些仿函数所产生的对象,用法和一般函数完全相同,当然我们还可以 产生无名的临时对象来履行函数功能。使用内建函数对象,需要引入头文件#include<functional>。

3、算数仿函数

//6个算数类函数对象,除了negate是一元运算,其他都是二元运算。 
template<class T> T plus<T>// 加 法 仿 函 数 
template<class T> T minus<T>// 减 法 仿 函 数 
template<class T> T multiplies<T>// 乘 法 仿 函 数 
template<class T> T divides<T>// 除 法 仿 函 数 
template<class T> T modulus<T>// 取 模 仿 函 数 
template<class T> T negate<T>// 取 反 仿 函 数 

4、关系仿函数

//6个关系运算类函数对象,每一种都是二元运算。 
template<class T> bool equal_to<T>// 等 于 
template<class T> bool not_equal_to<T>// 不 等 于 
template<class T> bool greater<T>// 大 于 
template<class T> bool greater_equal<T>// 大 于 等 于 
template<class T> bool less<T>// 小 于 
template<class T> bool less_equal<T>// 小 于 等 于 

5、逻辑仿函数

//逻辑运算类运算函数,not为一元运算,其余为二元运算。 
template<class T> bool logical_and<T>// 逻 辑 与 
template<class T> bool logical_or<T>// 逻 辑 或 
template<class T> bool logical_not<T>// 逻 辑 非

6、代码实例

①加法仿函数

void test02() 
{ 
    plus<int> p; 
    cout << p(10, 20) << endl;            //30
    cout<<plus<int>()(100,300)<<endl;    //300
}

使用内建函数对象 改变排序规则

void test04()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);

    for_each(v.begin(),v.end(),[](int val){cout<<val<<" ";});
    cout<<endl;

    //使用内建函数对象 改变排序规则
    sort(v.begin(),v.end(), greater<int>() );
    
    for_each(v.begin(),v.end(),[](int val){cout<<val<<" ";});
    cout<<endl;
}

③逻辑反函数

#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>

using namespace std;
void test01()
{
vector<bool> v;
v.push_back(true);
v.push_back(false);
v.push_back(true);
v.push_back(false);
for (vector<bool>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//逻辑非 将v容器搬运到v2中,并执行逻辑非运算
vector<bool> v2;
v2.resize(v.size());
transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++){
cout << *it << " ";
}
cout << endl;}
int main()
{
  test01();
  system("pause");
   return 0;
}
原文地址:https://www.cnblogs.com/mango1997/p/14569718.html