STL第三弹---函数对象

4.STL函数对象

4.1 函数对象

4.1.1 函数对象概念

概念:
·重载函数调用操作符的类,其对象常称为函数对象
·函数对象使用重载的()时,行为类似函数调用,也叫仿函数

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

4.1.2 函数对象使用

特点:
·函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值.
·函数对象超出普通函数的概念,函数对象可以有自己的状态
·函数对象可以作为参数传递

#include<iostream>
using namespace std;
class MyAdd
{
public:
	int operator()(int v1,int v2)
	{
		return v1 + v2;
	}
};

class MyPrint
{
public:
	MyPrint()
	{
		this->count = 0;
	}
	void operator()(string test)
	{
		cout << test << endl;
		this->count++;
	}
	int count;//内部记录自己的状态
};
//函数对象(仿函数)
void test01()
{
	MyAdd myadd;
	cout<<myadd(10, 10)<<endl;
}

void test02()
{
	MyPrint myprint;
	myprint("hello");
	myprint("hello");
	myprint("hello");
	myprint("hello");

	cout << "myPrint调用次数为:" << myprint.count << endl; 

}
//函数对象可以作为参数传递
void doPrint(MyPrint& mp, string test)
{
	mp(test);
}
void test03()
{
	MyPrint myprint;
	doPrint(myprint, "Hello c++");

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

4.2 谓词

4.2.1 谓词概念

概念:
·返回bool类型的仿函数称为谓词
·如果operator()接受一个参数,那么叫做一元谓词如果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);
	}

	//查找容器中有没有大于五的数字
	//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;
}

4.2.2 一元谓词

#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);
	}

	//查找容器中有没有大于五的数字
	//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;
}

4.2.3 二元谓词

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//二元谓词
class MyCompare
{
public:
	bool operator()(int val1,int val2)
	{
		return val1 > val2;
	}
};
void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(20);
	v.push_back(30);
	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());
	cout << "-----------------" << endl;
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

4.3 内建函数对象

4.3.1 内建函数对象意义

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

分类:
算术仿函数
关系仿函数
逻辑仿函数

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

4.3.2 算术仿函数

功能描述:
·实现四则运算
·其中negate是一元运算,其他都是二元运算

仿函数原型:
·templateT plus
//加法仿函数
·templateT minus
//减法仿函数
templateT ·multiplies
//乘法仿函数
templateT ·divides
//除法仿函数
templateT modulus
//取模仿函数
·templateT negate
//取反仿函数

#include<iostream>
using namespace std;
#include<functional>
//内建函数对象  算数仿函数

//negate 一元仿函数  取反仿函数
void test01()
{
	negate<int> n;
	cout << n(50) << endl;

}

//plus二元仿函数

void test02()
{
	plus<int> p;
	cout << p(10, 20) << endl;
}
int main()
{
	test02();
	system("pause");
	return 0;
}

4.3.3 关系仿函数

功能描述:
实现关系对比

仿函数原型:
templatebool equal to //等于
templatebool not equal to //不等于
template bool greatersT> //大于
template bool greater equal //大于等于
templatebool less //小于
template bool less equal //小于等于

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
//内建函数对象——关系仿函数
//大于greater
class MyCompare
{
public:
	bool operator()(int val1, int val2)
	{
		return val1 > val2;
	}
};
void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(20);
	v.push_back(30);
	v.push_back(50);
	sort(v.begin(), v.end(), MyCompare());
	sort(v.begin(), v.end(), greater<int>());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

最常用greater<>()大于

4.3.4 逻辑仿函数

功能描述:
实现逻辑运算

函数原型:
template bool logical and//逻辑与
templatebool logical or//逻辑或
templatebool logical not //逻辑非

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
void test01()
{
	vector <bool> v;
	v.push_back(true);
	v.push_back(false);	
	v.push_back(true);
	v.push_back(false);
	v.push_back(true);
	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.mfbz.cn/a/580959.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【C++杂货铺】二叉搜索树

目录 &#x1f308;前言&#x1f308; &#x1f4c1; 二叉搜索树的概念 &#x1f4c1; 二叉搜索树的操作 &#x1f4c2; 二叉搜索树的查找 &#x1f4c2; 二叉搜索树的插入 &#x1f4c2; 二叉搜书树的删除 &#x1f4c1; 二叉搜索树的应用 &#x1f4c1; 二叉搜索树的…

vue3 h5模板

vue3的h5模板 基于vue3tsvantrem的h5模板 觉得帮到你了就给个start

【win10移动热点,提示正在获取ip地址...】

检查 Wired AutoConfig/ WLAN AutoConfig 服务运行 电脑→管理→服务和应用程序→服务&#xff1a;AutoConfig 有线网络无线网卡 1.开启wifi热点&#xff0c;自动生成“本地连接*10”&#xff1b; 2.配置Wired LAN网络共享 仅无线网卡 1. 开启wifi热点&#xff0c;自动生…

自学Python爬虫js逆向(二)chrome浏览器开发者工具的使用

js逆向中很多工作需要使用浏览器中的开发者工具&#xff0c;所以这里以chrome为例&#xff0c;先把开发者工具的使用总结一下&#xff0c;后面用到的时候可以回来查询。 Google Chrome浏览器的开发者工具是前端开发者的利器&#xff0c;它不仅提供了丰富的功能用于开发、调试和…

电动公共交通车充电站设置储能电站方案

相比于传统燃油公交车&#xff0c;电动公交车以电代油&#xff0c;具有零排放、低噪音、低能耗等优势。随着电池、车载电机等相关技术的不断发展&#xff0c;电动公交车在国内乃至全世界范围内逐步推广应用。动力电池是电动公交车的重要部件之一&#xff0c;其寿命及性能影响着…

解密推理部署工程师的必备技能,面试题库分析

推理部署工程师面试题库 1. 描述一下SM的结构&#xff1f; 英伟达 GPU 架构&#xff1a;* 计算核心&#xff1a;INT32、FP32、FP64 CUDA 核心&#xff0c;Tensor 核心&#xff0c;超低延迟负载/存储。* 调度和存储器&#xff1a;Warp 调度器注册文件&#xff0c;共享存储器&am…

逻辑回归模型与GBDT+LR——特征工程模型化的开端

随着信息技术和互联网的发展&#xff0c; 我们已经步入了一个信息过载的时代&#xff0c;这个时代&#xff0c;无论是信息消费者还是信息生产者都遇到了很大的挑战&#xff1a; 信息消费者&#xff1a;如何从大量的信息中找到自己感兴趣的信息&#xff1f;信息生产者&#xff…

主题乐园私域精细化运营

主题乐园私域精细化运营是指在细分用户群体的基础上&#xff0c;通过个性化、精准的运营方式&#xff0c;为用户提供定制化服务和体验。以下是一些常见的主题乐园私域精细化运营玩法&#xff1a; 会员制度和会员专属服务&#xff1a;建立完善的会员制度&#xff0c;为会员提供专…

碳实践 | 一文读懂LCA产品生命周期环境影响评价

一、产品生命周期评价定义 生命周期评价&#xff1a;生命周期评价&#xff08;Life Cycle Assessment&#xff0c;简称LCA&#xff09;是一种量化评价方法。它涵盖了产品的整个生命周期——从自然资源开采到原材料加工、产品制造、分销、使用&#xff0c;直至最终废弃处置或回…

mongodb使用debezium

前置 服务器上需要安装jdk11 jdk下载地址 kafka安装 官网下载地址 安装教程 debezium 安装 运行 Debezium 连接器需要 Java 11 或更高版本 Debezium 并不是一个独立的软件&#xff0c;而是很多个 Kafka 连接器的总称。这些 Kafka 连接器分别对应不同的数据库&#xff0c;…

使用Cesium ion将 Sketchfab 3D 模型添加到您的GIS应用中

您现在可以将 Sketchfab 中的 3D 模型导入 Cesium ion 中以创建 3D 块&#xff0c;从而更轻松地为地理空间体验创建上下文和内容。 Sketchfab 是 Epic Games 的一部分&#xff0c;也是使用最广泛的 3D 资产市场之一。自 2012 年推出以来&#xff0c;已有超过 1000 万用户使用 …

2024/4/28 C++day5

有以下类&#xff0c;完成特殊成员函数 class Person { string name; int *age; } class Stu:public Person { const double score; } #include <iostream> #include <string> using namespace std; class Person { string name; int *age ; publi…

Kafka报错ERROR Exiting Kafka due to fatal exception during startup

报错&#xff1a; ERROR Exiting Kafka due to fatal exception during startup. (kafka.Kafka$) kafka.common.InconsistentClusterIdException: The Cluster ID FSzSO50oTLCRhRnRylihcg doesnt match stored clusterId Some(0oSLohwtQZWbIi73YUMs8g) in meta.properties. Th…

手撕红黑树(kv模型模拟)

目录 前言 一、相关概念 二、性质介绍 红黑树平衡说明 三、红黑树模拟&#xff08;kv结构&#xff09; 1、红黑树节点 2、红黑树插入 2、特殊处理情况 声明&#xff1a; 情况一&#xff1a;cur为红&#xff0c;p为红&#xff0c;g为黑&#xff0c;u存在&#xff0c;且…

MyBatis 核心配置讲解(下)

大家好&#xff0c;我是王有志&#xff0c;一个分享硬核 Java 技术的互金摸鱼侠。 我们书接上回&#xff0c;继续聊 MyBatis 的核心配置&#xff0c;我们今天分享剩下的 5 项核心配置。 不过正式开始前&#xff0c;我会先纠正上一篇文章 MyBatis 核心配置讲解&#xff08;上&…

QAnything知识库问答系统离线部署(LLM+RAG)

一、QAnything介绍 &#xff08;一&#xff09;简介 QAnything 是网易有道开源的一个问答系统框架&#xff0c;支持私有化部署和SaaS服务两种调用形式。它能够支持多种格式的文件或数据库&#xff0c;提供准确、快速和可靠的问答体验。目前已支持的文件格式包括PDF、Word、PP…

防火墙对要保护的服务器做端口映射的好处是几个

防火墙对要保护的服务器进行端口映射具有多重好处&#xff0c;这些好处主要围绕网络安全性、灵活性和可管理性展开。以下是对这些好处的专业分析&#xff1a; 1. 增强网络安全性&#xff1a;端口映射允许防火墙对进入服务器的流量进行精确控制。通过映射特定端口&#xff0c;防…

FPGA秋招-笔记整理(3)无符号数、有符号数

参考&#xff1a;Verilog学习笔记——有符号数的乘法和加法 一、无符号数、有符号数 将输入输出全部定义为有符号数 &#xff08;1&#xff09;无符号数的读取按照原码进行&#xff0c;有符号数的读取应该按照补码读取&#xff0c;计算规则为去掉符号位后取反、加1在计算数值…

Flink学习(九)-jar 包提交给 flink 集群执行

一、界面执行 1&#xff0c;点击左侧的 submit new job&#xff0c;然后点击add New 2&#xff0c;粘贴程序入口&#xff0c;设置并行度 3&#xff0c;执行后&#xff0c;就可以在 taskManager 中找到相关任务了 二、控制台执行 在命令行中&#xff0c;在flink 的安装目录下&…

【Java】关于异常你需要知道的事情

文章目录 异常体系异常声明捕获多个异常Java中的哪些异常&#xff0c;程序不用捕获处理&#xff1f;【重要】try with resource 异常处理流程foreach中遇到异常面试题try和finally中都由return 异常体系 异常声明 如果声明的是Exception&#xff0c;那么必须要处理如果声明的是…
最新文章