STL-函数对象

1.函数对象

1.1 概念

重载函数调用操作符的类,其对象被称为函数对象

函数对象使用重载的()时,行为类似函数调用,也成为仿函数

本质:函数对象(仿函数)是一个类,不是一个函数

1.2 函数对象的使用

在使用时可以像普通函数一样调用,可以有参数和返回值

函数对象可以有自己的状态

函数对象可以作为参数传递

cpp 复制代码
#include <iostream>
#include <vector>

using namespace std;

class Myadd{
    public:
    int operator()(int x,int y){
        return x+y;
    }
};

void test1(){
    Myadd ma;
    cout<<ma(10,10)<<endl;
}

//可以有自己的状态
class MyPrint{
    public: 
    MyPrint(){
        count=0;
    }
    void operator()(string test){
        cout<<test<<endl;
        count++;
    }
    int count;
};

void test2(){
    MyPrint mp;
    mp("hello world");
    mp("hello world");
    mp("hello world");
    cout<<"调用了"<<mp.count<<"次"<<endl;
}

// 可以作为参数传递
void DoPrint(MyPrint &mp,string test){
    mp(test);
}

void test3(){
    MyPrint mp;
    DoPrint(mp,"hello world");
}

int main()
{
    test1();
    test2();
    test3();
    system("pause");
    return 0;
}

2.谓词

2.1 概念:

返回bool类型的仿函数称为谓词

如果operator()接收一个参数成为一元谓词

如果operator()接收两个参数成为二元谓词

2.2 一元谓词

一元谓词(Unary Predicate) 是一个接受一个参数并返回布尔值(truefalse 的函数对象或函数。

其核心作用是:对单个输入值进行条件判断,返回是否满足特定条件。

cpp 复制代码
#include <iostream>
#include <vector>
#include<algorithm>

using namespace std;

struct CreateFive{
    bool operator()(int x){
        return x>5;
    }
};

void test(){
    vector<int> v;
    for(int i=0;i<10;i++){
        v.push_back(i);
    }

    vector<int>::iterator it = find_if(v.begin(),v.end(),CreateFive());// 查找满足CreateFive的元素,指向第一个满足条件的位置
    if (it==v.end()){
        cout<<"没有大于5的元素"<<endl;
    }else{
        cout<<*it<<endl;//只输出一个6
    }
}

int main()
{
    test();
    system("pause");
    return 0;
}

2.3 二元谓词

二元谓词(Binary Predicate) 是一个接受两个参数并返回布尔值(truefalse 的函数对象或函数。

其核心作用是:对两个输入值进行条件判断,返回是否满足特定关系。

cpp 复制代码
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;

class MyCompare{
    public:
        bool operator()(int v1,int v2){
            return v1>v2;
        }
        // 这个函数接受两个参数(v1 和 v2),返回一个布尔值(true 或 false),
        // 表示 v1 是否大于 v2。
};


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

    sort(v.begin(),v.end());
    for(vector<int>::iterator it=v.begin();it!=v.end();it++){
        cout<<*it<<" ";   
    }cout<<endl;

    //改变排序顺序
    sort(v.begin(),v.end(),MyCompare());
    for(vector<int>::iterator it=v.begin();it!=v.end();it++){
        cout<<*it<<" ";   
    }cout<<endl;

}

int main()
{
    test();
    system("pause");
    return 0;
}

3.内建函数对象

3.1意义

STL内建了一些函数对象

需要在头文件#include<functional>

3.2算数仿函数

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>;//取反

cpp 复制代码
#include <iostream>
#include <functional>

using namespace std;

//negate
void Negate(){
    negate<int> n;
    cout<<n(10)<<endl;
}

void Add(){
    plus<int> p;
    cout<<p(10,20)<<endl;
}

int main()
{
    Negate();
    Add();
    system("pause");
    return 0;
}

3.3关系仿函数

实现关系对比

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 less<T>// 小于

template<class T> bool greater_equal<T>// 大于等于

template<class T> bool less_equal<T>// 小于等于

cpp 复制代码
#include <iostream>
#include <vector>
#include<functional>
#include<algorithm>

using namespace std;

class MyCompare{
    public:
    bool operator()(int x,int y){
        return x>y;
    }
};

void test(){
    vector<int> v;
    v.push_back(10);
    v.push_back(80);
    v.push_back(30);
    v.push_back(50);
    v.push_back(70);

    for(vector<int>::iterator it=v.begin();it!=v.end();it++){
        cout<<*it<<" ";
    }cout<<endl;

    sort(v.begin(),v.end(),MyCompare());

    //STL内建仿函数
    sort(v.begin(),v.end(),greater<int>());
    for(vector<int>::iterator it=v.begin();it!=v.end();it++){
        cout<<*it<<" ";
    }cout<<endl;
}

int main()
{
    test();
    system("pause");
    return 0;
}

3.4逻辑仿函数

template<class T> bool logical_and<T>// 逻辑与

template<class T> bool logical_or<T>// 逻辑或

template<class T> bool logical_not<T>// 逻辑非

cpp 复制代码
#include <iostream>
#include <vector>
#include<functional>
#include<algorithm>

using namespace std;

void test(){
    vector<bool> v;
    v.push_back(true);
    v.push_back(false);
    v.push_back(true);
    v.push_back(false);

    for(auto it=v.begin();it!=v.end();it++){
        cout<<*it<<" ";
    }cout<<endl;

    vector<bool> v2;
    v2.resize(v.size());//将v2的大小设置为v的大小
    transform(v.begin(),v.end(),v2.begin(),logical_not<bool>());
    //遍历v,从v2的begin开始,将v的值取反,然后赋值给v2
    for(auto it=v2.begin();it!=v2.end();it++){
        cout<<*it<<" ";
    }cout<<endl;
}

int main()
{
    test();
    system("pause");
    return 0;
}
相关推荐
CryptoRzz10 分钟前
日本股票 API 对接实战指南(实时行情与 IPO 专题)
java·开发语言·python·区块链·maven
yugi98783811 分钟前
基于M序列的直扩信号扩频码生成方法及周期长码直扩信号的MATLAB实现方案
开发语言·matlab
旧梦吟15 分钟前
脚本网页 三人四字棋
前端·数据库·算法·css3·html5
乾元18 分钟前
基于时序数据的异常预测——短期容量与拥塞的提前感知
运维·开发语言·网络·人工智能·python·自动化·运维开发
江上清风山间明月18 分钟前
使用python将markdown文件生成pdf文件
开发语言·python·pdf
凯_kyle19 分钟前
Python 算法竞赛 —— 基础篇(更新ing)
笔记·python·算法
j_xxx404_21 分钟前
C++算法入门:二分查找合集(二分查找|在排序数组中查找元素的第一个和最后一个位置)
开发语言·c++
ss27324 分钟前
阻塞队列:ArrayBlockingQueue如何用Lock与Condition实现高效并发控制
开发语言·python
lizz3127 分钟前
C++操作符重载深度解析
java·c++·算法
CodeCraft Studio28 分钟前
Vaadin 25 正式发布:回归标准Java Web,让企业级开发更简单、更高效
java·开发语言·前端·vaadin·java web 框架·纯java前端框架·企业级java ui框架