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;
}
相关推荐
举一个梨子zz1 分钟前
Java—— 网络爬虫
java·开发语言·爬虫·intellij-idea·需求分析
伍贰什丿3 分钟前
python学习day2:运算符+优先级
开发语言·python·学习
YGGP17 分钟前
动态规划之爬楼梯模型
算法·动态规划
muzi_liii17 分钟前
C++11-(3)
开发语言·c++
南瓜胖胖28 分钟前
R语言开始绘图--柱状图
开发语言·r语言
Unique27632 分钟前
Linux条件变量
linux·开发语言
stoneSkySpace32 分钟前
算法--js--组合总和
前端·javascript·算法
老歌老听老掉牙36 分钟前
Open CASCADE学习|非线性方程组求解技术详解
c++·学习·opencascade·非线性方程组
潇-xiao43 分钟前
Qt window frame + windowTitle + windowIcon属性(3)
c++·笔记·qt
A charmer1 小时前
C++ 日志系统实战第六步:性能测试
c++·日志系统