STL - 函数对象

STL - 函数对象

一、函数对象
1.概念
  • 重载函数调用操作符的类,其对象常成为函数对象
  • 函数对象使用重载的()时,行为类似函数调用,也叫仿函数

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

2.函数对象使用

特点:

  • 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
  • 函数对象可以作为参数传递
c++ 复制代码
//1
class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};
void test01()
{
	//使用类创建对象,这里myAdd叫函数对象
	MyAdd myAdd;
	cout << myAdd(7, 8) << endl;//15
	//函数对象可以像函数一样调用
}
//2
class MyPrint
{
public:
	MyPrint()
	{
		this->count = 0;
	}
	void operator()(string test)
	{
		cout << test << endl;
		this->count++;
	}
	int count;//记录状态,内部自己的状态
};
//有自己的状态
void test02()
{
	MyPrint myPrint;
	myPrint("Hello! welcom to qingqingcaoyuan");
	cout << "myPrint调用次数:"<<myPrint.count << endl;
}

//3.函数对象可以作为参数传递
void doPrint(MyPrint& mp, string test)
{
	mp(test);
}
void test03()
{
	MyPrint myPrint;
	doPrint(myPrint, "lanyy1");
}
二、谓词
1.概念
  • 返回bool类型的仿函数称为谓词
  • 如果operator()接受一个参数,那么叫做一元谓词
  • 如果operator()接受两个参数,那么叫做二元谓词
2.一元谓词
c++ 复制代码
class GreaterFive
{
public:
	bool operator()(int val)//一元谓词
	{
		return val > 6;
	}
};
void test04()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	//查找容器中是否存在大于6的数字
	//find_if,条件查找算法,返回的是一个迭代器
	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());//最后一个参数为匿名的函数对象,是我们要查找的条件
	if (it == v.end())
	{
		cout << "cannot find !" << endl;
	}
	else
	{
		cout << "find it ,the value is: " << *it << endl;
	}
}

函数参数若是 pred 就代表这里需要填写一个谓词

3.二元谓词
C++ 复制代码
class myCompare
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};
void test05()
{
	vector<int>v;
	v.push_back(7);
	v.push_back(8);
	v.push_back(3);
	v.push_back(6);
	v.push_back(5);

	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;
}
三、内建函数对象
1.基本概念

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

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

用法:

仿函数所产生的对象,用法和一般函数完全相同

使用内建函数对象,需要引入头文件 #include<functional>

2.算术仿函数

实现四则运算;其中negate是一元运算(一个操作数),其他都是二元运算。仿函数原型:

c++ 复制代码
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>       //取反仿函数

使用示例:

c++ 复制代码
void test06()
{
	//取反 一元仿函数
	negate<int>n;//数据类型为int,数据名为n
	cout << n(7) << endl;

	//二元仿函数
	plus<int>m;//默认认为传入的都是同种类型,只允许同类型相加
	cout << m(7, 8) << endl;
	//其余运算只需要改变plus即可
}
3.关系仿函数

实现关系对比的操作,函数原型:

c++ 复制代码
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>	//小于等于

示例:

c++ 复制代码
void test07()
{
	//举例大于greater
	vector<int>v;
	v.push_back(7);
	v.push_back(8);
	v.push_back(6);
	v.push_back(9);
	v.push_back(5);

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	//降序排序,默认排序是升序排序,想要降序排序,需要添加一个bool类型的谓词,默认是小于less
	//sort(v.begin(), v.end(), myCompare());//这是自己写的仿函数,其次可以使用编译器内建的仿函数
	sort(v.begin(),v.end(),greater<int>());//greater<int>()是内建的仿函数
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
4.逻辑仿函数

实现逻辑运算,函数原型:

c++ 复制代码
template<class T> bool logical_and<T>	//逻辑与
template<class T> bool logical_or<T>	//逻辑或
template<class T> bool logical_not<T>	//逻辑非

示例:

c++ 复制代码
void test08()
{
	//主要举例逻辑非
	vector<bool>v;
	v.push_back(true);
	v.push_back(false);
	v.push_back(false);

	for (vector<bool>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << "   ";
	}
	cout << endl;

	//利用逻辑非取反放入另一个容器中
	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;
}
相关推荐
电商API_180079052471 分钟前
京东商品评论API接口封装的心路历程
服务器·开发语言·爬虫·数据分析·php
扶尔魔ocy11 分钟前
【转载】QT使用linuxdeployqt打包
开发语言·qt
醒过来摸鱼15 分钟前
合并区间问题
算法
Trouvaille ~16 分钟前
【动态规划篇】专题(二):路径问题——在网格图中的决策艺术
c++·算法·leetcode·青少年编程·动态规划
好家伙VCC26 分钟前
# 发散创新:基于Python的TTS语音合成实战与优化策略 在人工智能加速落地的今天,**文本转
java·开发语言·人工智能·python
货拉拉技术43 分钟前
文本大模型评测实践
人工智能·深度学习·算法
等D春C夏X43 分钟前
最终版C++11/14/17学习大纲(精准核对42条条款)
java·开发语言
CoovallyAIHub1 小时前
模糊、噪声、压缩……让检测器学会主动评估画质
深度学习·算法·计算机视觉
跃龙客1 小时前
atomic笔记
笔记·算法