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;
}
相关推荐
风掣长空2 分钟前
[leetcode]第445场周赛
数据结构·算法·leetcode
路过刷题5 分钟前
第16届蓝桥杯c++省赛c组个人题解
c语言·c++·蓝桥杯
虾球xz11 分钟前
游戏引擎学习第222天
c++·学习·游戏引擎
郜太素19 分钟前
决策树+泰坦尼克号生存案例
人工智能·算法·决策树·机器学习·数据挖掘·学习方法
Wind哥22 分钟前
C++在Linux上生成动态库并调用接口测试
linux·服务器·c++
脏脏a27 分钟前
【排序算法】快速排序
数据结构·算法
心.c37 分钟前
【贪心之摆动序列】
数据结构·算法·leetcode
搞不懂语言的程序员37 分钟前
迭代器模式深度解析与实战案例
开发语言·python·迭代器模式
SRC_BLUE_171 小时前
Python GUI 编程 | QAbstractButton 抽象按钮类详解 — 按钮状态设置
开发语言·python
烁3471 小时前
每日一题(小白)暴力娱乐篇24
算法·娱乐