STL-函数对象(仿函数)

简介: STL-函数对象(仿函数)

概念:

  • 重载函数调用操作符的类,其对象被称为函数对象
  • 函数对象使用重载的()时,行为类似函数调用,也叫仿函数

本质:

  • 函数对象(仿函数)是一个类,不是一个函数

函数对象使用:

  • 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
  • 函数对象可以作为参数传递
#include<iostream>
#include<string>
using namespace std;
//函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
class myAdd
{
public:
    int operator()(int v1, int v2)
    {
        return v1 + v2;
    }
};
void test1()
{
    myAdd a1;
    cout << a1(1, 2) << endl;
}
//函数对象超出普通函数的概念,函数对象可以有自己的状态
class myPrint
{
public:
    myPrint()
    {
        this->count = 0;
    }
    void operator()(string str)
    {
        cout << str << endl;
        count++;
    }
    int count;
};
void test2()
{
    myPrint p1;
    p1("A");
    p1("A");
    p1("A");
    cout << p1.count << endl;
}
//函数对象可以作为参数传递
void doPrint(myPrint& mp, string str)
{
    mp(str);
}
void test3()
{
    myPrint mp;
    doPrint(mp, "hello");
}
int main()
{
    test1();
    test2();
    test3();
    return 0;
}

谓词

  • 返回值是bool类型的仿函数被称为谓词
  • 如果operator()接受一个参数,那么叫做一元谓词
  • 如果operator()接受两个参数,那么叫做二元谓词

一元谓词举例

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class GreaterFive
{
public:
    bool operator()(int val)
    {
        return val > 5;
    }
};
int main()
{
    vector<int>v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    //查找容器中大于5的元素
    //GreaterFive() 匿名函数对象
    vector<int>::iterator it=find_if(v.begin(), v.end(), GreaterFive());
    cout << *it << endl;
    return 0;
}

二元谓词举例

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class myCompare
{
public:
    bool operator()(int v1, int v2)
    {
        return v1 > v2;
    }
};
int main()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(20);
    v.push_back(40);
    //默认sort升序排序
    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 << " ";
    }
    return 0;
}

内建函数对象

STL内建了一些函数对象:

  • 算术仿函数
  • 关系仿函数
  • 逻辑仿函数

用法:

  • 这些仿函数所产生的对象,用法和一般函数完全相同
  • 使用内建仿函数,需要引入头文件<functional>

算术仿函数

其中negate是一元运算,其他都是二元运算:

  • template<class T> T puls<T>//加法
  • template<class T> T minus<T>//减法
  • template<class T> T multiplies<T>//乘法
  • template<class T> T modulus<T>//除法
  • template<class T> T negate<T>//取反
#include<iostream>
#include<functional>
using namespace std;
int main()
{
    //取反
    negate<int>n;
    cout << n(50) << endl;//-50
    cout << n(0) << endl;//0
    //加法 只填一个模板参数,默认相同类型
    plus<int>p;
    cout << p(10, 20) << endl;
    return 0;
}

关系仿函数

  • 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>//小于等于

sort默认的排序规则是从小到大,底层默认使用了less<T>

#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(40);
    v.push_back(20);
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << endl;
    }
    sort(v.begin(), v.end(), less<int>());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << endl;
    }
    return 0;
}

逻辑仿函数

  • template<class T> bool logical_and<T>逻辑与
  • template<class T> bool logical_or<T>逻辑或
  • template<class T> bool logical_not<T>逻辑非
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
    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搬运到容器v1中,并执行取反操作
    vector<bool>v2;
    v2.resize(v.size());
    //注意没有 v2.end()
    transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
    for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
    {
        cout << *it << " ";
    }
    return 0;
}
目录
相关文章
|
Java Linux 开发工具
Linux下安装jdk之Centos
Linux下安装jdk之Centos
336 2
|
Linux 数据安全/隐私保护 网络虚拟化
|
资源调度 运维 Kubernetes
揭开阿里巴巴复杂任务资源混合调度技术面纱
作为更进一步的云计算形态,云原生正在成为云时代的技术新标准,通过重塑整个软件生命周期,成为释放云价值的最短路径。
10500 12
揭开阿里巴巴复杂任务资源混合调度技术面纱
|
数据采集 算法 容器
一文带你了解dfs和bfs算法
dfs算法又称深度优先搜索,是计算机术语。 1、dfs是一种在开发爬虫早期使用较多的方法,是搜索算法的一种。 2、dfs的目的是要达到被搜索结构的叶结点,即那些不包含任何超链的HTML文件。 3、dfs根据已有的邻接矩阵或邻接表用递归方法编写深度优先搜索遍历算法,并输出遍历结果 作为搜索算法的一种,DFS对于寻找一个解的NP(包括NPC)问题作用很大。但是,搜索算法毕竟是时间复杂度是O(n!)的阶乘级算法,它的效率非常低,在数据规模变大时,这种算法就显得力不从心了。当节点v的所有边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点。这一过程一直进行到已发现从源节点可达的所有节点为止
1193 0
一文带你了解dfs和bfs算法
|
数据库 关系型数据库 Oracle
OCP-052考试题库汇总(32)-CUUG内部解答版
Which state must a database be to enable ARCHIVELOG ? A)NOMOUNT B)OPEN IN READ WRITE mode C)OPEN IN READ ONLY mode D)OPEN IN RESTRICTED mode E)MOUNT ...
910 0
|
机器学习/深度学习 存储 Windows
将一个十六进制字符串转换为十进制数值的问题
在程序中,我们有时需要将一个十六进制字符串转换为十进制数字。比如:char *ptr="0x11";int n=0;//我们想让n等于0x11,即17 通常我们在C中,想将一个字符串转换为一整形数字,通常会使用下面的方法:   char *ptr="123";       int n=0;...
2945 0
|
Web App开发 Java
开源框架:Structs2.0入门学习(2)
  3.介绍一下常用的标签库        Struts 1.x  Struts 2.0  分类  将标志库按功能分成HTML、Tiles、Logic和Bean等几部分  严格上来说,没有分类,所有标志都在URI为“/struts-tags”命名空...
782 0
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16921 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置