c++学习笔记-提高篇-STL-函数对象

目录

一、函数对象

二、函数对象使用

三、谓词

1、概念

2、一元谓词

3、二元谓词

插入一条sort函数源码

四、内建函数对象

1.基本概念

2、算数仿函数

3、关系仿函数

4、逻辑仿函数


一、函数对象

函数对象概念

(1)重载函数调用操作符的类,其对象常称为函数对象

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

本质:

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

二、函数对象使用

特点:

  • 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
  • 函数对象可以作为参数传递
#include<iostream>
using namespace std;/*函数对象(仿函数)
函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
函数对象超出普通函数的概念,函数对象可以有自己的状态
函数对象可以作为参数传递
*/class MyAdd
{
public:int operator()(int v1, int v2){return v1 + v2;}
};//1、函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
void test01()
{MyAdd myadd;cout << myadd(10, 10) << endl;
}//2、函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint
{
public:MyPrint(){this->count = 0;}void operator()(string test){cout << test << endl;count++;}int count;   //内部自己状态
};void test02()
{MyPrint myprint;myprint("hello world");myprint("hello world");myprint("hello world");myprint("hello world");myprint("hello world");cout << "MyPrint调用次数为:" << myprint.count << endl;}//3、函数对象可以作为参数传递
void doPrint(MyPrint& mp, string test)
{mp(test);
}void test03()
{MyPrint myPrint;doPrint(myPrint, "Hello c++");
}int main()
{//test01();//test02();test03();system("pause");return 0;}

三、谓词

1、概念

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

2、一元谓词

operator()接受一个参数

示例:

#include<iostream>
using namespace std;#include<vector>
#include<algorithm>
//仿函数 返回值类型是bool数据类型,称为谓词
//一元谓词class GreaterFive
{
public:bool operator()(int val){return val > 5;}
};void test01()
{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());if (it == v.end()){cout << "未找到" << endl;}else{cout << "找到了大于5的数字为:" << *it << endl;}
}int main()
{test01();system("pause");return 0;
}

3、二元谓词

operator()接受两个参数

示例:

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//二元谓词class myCompare
{
public:bool operator()(int v1, int v2){return v1 > v2;}
};void test01()
{vector<int> v;v.push_back(10);v.push_back(40);v.push_back(30);v.push_back(20);v.push_back(50);sort(v.begin(),v.end());//默认排序从小到大for (vector<int>::iterator it = v.begin(); it != v.end(); it++){cout << *it<<"  ";}cout << endl;//使用函数对象  改变算法策略,变为排序规则为从大到小  Pred就是谓词sort(v.begin(), v.end(), myCompare());cout << "------------------------------" << endl;for (vector<int>::iterator it = v.begin(); it != v.end(); it++){cout << *it << "  ";}cout << endl;}int main()
{test01();system("pause");return 0;
}

插入一条sort函数源码

sort函数两个重载版本如下图

  • 第一个重载版本是

_CONSTEXPR20 void sort(const _RanIt _First, const _RanIt _Last, _Pr _Pred)

参数1和参数2为迭代器表示的区间范围,参数3“_Pred”是谓词

  • 第二个重载版本是

_CONSTEXPR20 void sort(const _RanIt _First, const _RanIt _Last)

参数1和参数2为迭代器表示的区间范围

四、内建函数对象

1.基本概念

(1)概念:STL内建了一些函数对象

(2)分类

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

(3)用法

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

2、算数仿函数

(1)功能描述

  • 实现四则运算
  • 其中negate是一元运算,其他都是二元运算

(2)仿函数原型

  • 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>            //取反仿函数

(3)示例:加法、取反的使用

#include<iostream>
#include<functional>   //使用STL内建的函数对象 头文件
using namespace std;//内建函数对象  算数仿函数//nagete  一元仿函数  取反仿函数
void test01()
{negate<int>n;cout << n(50) << endl;
}//plus    二员仿函数  加法
void test02()
{plus<int>p;  //注意此处是同种数据类型cout << p(10, 20) << endl;
}using namespace std;int main()
{//test01();test02();system("pause");return 0;
}

3、关系仿函数

(1)功能描述:实现关系对比

(2)仿函数原型

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

(3)示例:大于的使用

#include<iostream>
#include<functional>
#include<vector>
using namespace std;
#include<algorithm>
#include<functional>//内建的函数对象  _关系仿函数class myCompare 
{
public:bool operator()(int v1,int v2){return v1 > v2;}
};void test01()
{vector<int> v;v.push_back(10);v.push_back(30);v.push_back(40);v.push_back(20);v.push_back(50);for (vector<int>::iterator it = v.begin(); it != v.end(); it++){cout << *it << "  ";}cout << endl;//降序排序sort(v.begin(), v.end(),myCompare());cout << "-------------------------";cout << "降序排序" << endl;for (vector<int>::iterator it = v.begin(); it != v.end(); it++){cout << *it << "  ";}cout << endl;
}void test02()
{vector<int> v;v.push_back(10);v.push_back(30);v.push_back(40);v.push_back(20);v.push_back(50);for (vector<int>::iterator it = v.begin(); it != v.end(); it++){cout << *it << "  ";}cout << endl;//降序排序sort(v.begin(), v.end(), greater<int>());cout << "-------------------------";cout << "使用内建函数对象  降序排序" << endl;for (vector<int>::iterator it = v.begin(); it != v.end(); it++){cout << *it << "  ";}cout << endl;
}int main()
{//test01();test02();system("pause");return 0;
}

 注意:sort函数的源码中可以看到,默认排序用的less<>,也就是小于(v1<v2),所以默认排序时升序哦!

4、逻辑仿函数

(1)功能描述:实现逻辑运算

(2)仿函数原型:

  • template<class T> bool logical_and<T>                  //逻辑与
  • template<class T> bool logical_or<T>                     //逻辑或
  • template<class T> bool logical_not<T>                   //逻辑非

(3)示例:逻辑非的使用

#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
using namespace std;//内建函数对象  logical_notvoid 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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/309228.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

web一些实验代码——Servlet请求与响应

实验4&#xff1a;Servlet请求与响应 1、在页面输入学生学号&#xff0c;从数据库中查询学生信息并显示。 &#xff08;1&#xff09;启动MySQL数据库服务&#xff0c;新建数据库&#xff0c;将student.sql文件导入到新建数据库&#xff08;建立表&#xff0c;并插入3条数据&…

MySQL:子查询

子查询 子查询是嵌套在较大查询中的 SQL 查询&#xff0c;也称内部查询或内部选择&#xff0c;包含子查询的语句也称为外部查询或外部选择。简单来说&#xff0c;子查询就是指将一个 select 查询&#xff08;子查询&#xff09;的结果作为另一个 SQL 语句&#xff08;主查询&a…

「Verilog学习笔记」序列检测器(Moore型)

专栏前言 本专栏的内容主要是记录本人学习Verilog过程中的一些知识点&#xff0c;刷题网站用的是牛客网 timescale 1ns/1nsmodule det_moore(input clk ,input rst_n ,input din ,output reg Y ); parameter S0 …

3d导入模型怎样显示原本材质---模大狮模型网

要在导入3D模型时保留原本的材质&#xff0c;您可以尝试以下方法&#xff1a; 导入前检查文件格式&#xff1a;确保您所使用的3D软件支持导入模型的文件格式。不同的软件对文件格式的支持有所差异&#xff0c;选择正确的文件格式可以更好地保留原始材质。 使用正确的材质库&am…

智能透明加密、半透明加密和落地加密的区别是什么?

智能透明加密、半透明加密和落地加密的主要区别如下&#xff1a; PC端访问地址&#xff1a; https://isite.baidu.com/site/wjz012xr/2eae091d-1b97-4276-90bc-6757c5dfedee 保护对象和方式&#xff1a; 智能透明加密&#xff1a;系统根据预设的敏感数据特征&#xff0c;对正…

Dataframe合并

一、横向合并 1. id相同&#xff0c;列不同 on: 用于指定两个DataFrame之间合并的列名。how: 这个参数用于指定合并的类型。常见的值有inner, outer, 和 left。 – inner: 只保留两个DataFrame中都有的键值&#xff08;基于on参数指定的列&#xff09;。 – outer: 保留两个Da…

学习记录——BiFormer

BiFormer Vision Transformer with Bi-Level Routing Attention BiFormer:具有双电平路由注意的视觉变压器 摘要作为视觉转换器的核心组成部分,注意力是捕捉长期依赖关系的有力工具。然而,这样的能力是有代价的:当计算跨所有空间位置的成对令牌交互时,它会产生巨大的计算负…

Spring的Bean你了解吗

Bean的配置 Spring容器支持XML(常用)和Properties两种格式的配置文件 Spring中XML配置文件的根元素是,中包含了多个子元素&#xff0c;每个子元素定义了一个Bean,并描述了该Bean如何装配到Spring容器中 元素包含了多个属性以及子元素&#xff0c;常用属性及子元素如下所示 i…

金三银四-JAVA核心知识高频面试题

又要快到一年一度的金三银四&#xff0c;开始复习啦&#xff5e;&#xff01; 每天一点点。。 目录 一、内存模型设计 二、synchronized和ReentrantLock的区别 三、垃圾回收机制 四、优化垃圾回收机制 4.1 了解应用需求 4.2. 调整堆大小 4.3. 减少对象分配 4.4. 使用合…

RestClient操作索引库_创建索引库(二)

ES官方提供了各种不同语言的客户端&#xff0c;用来操作ES。这些客户端的本质就是组装DSL语句&#xff0c; 通过http请求发送给ES。 官方文档地址: https://www.elastic.co/quide/en/elasticsearch/client/index.html 目录 一、初始化JavaRestClient 1.1.依赖引入 1.2.初始化…

推荐几个开源HTTP服务接口快速生成工具

在现在流行微服务、前后端分离软件开发架构下&#xff0c;基于标准RESTful/JSON的HTTP接口已经成为主流。在实际业务中有很多需要快速开发调用数据服务接口的需求&#xff0c;但团队中缺乏专业的后端开发人员&#xff0c;比如&#xff1a; &#xff08;1&#xff09;数据库表已…

BFC 2023年度星光之夜即将开启,打造梦幻跨年盛典

跨年钟声即将敲响&#xff0c;星光繁花璀璨绽放。2023年12月31日&#xff0c;BFC外滩金融中心&#xff08;下称BFC&#xff09;年度星光之夜拉开帷幕&#xff0c;在热酒派对和星光音乐会的热烈节日氛围中&#xff0c;幸运气球将在全场传递节日祝福&#xff0c;更有惊喜好礼抽奖…