全球最实用的IT互联网信息网站!

AI人工智能P2P分享&下载搜索网页发布信息网站地图

当前位置:诺佳网 > 电子/半导体 > 嵌入式技术 >

c++之函数对象与内建函数

时间:2023-07-17 09:46

人气:

作者:admin

标签: C++  函数 

导读:重载函数调用操作的类,其对象常称之为函数对象。...

1.函数对象

  • 函数对象(仿函数):
    重载函数调用操作的类,其对象常称之为函数对象;
    函数对象使用重载()时,其行为类似函数调用,也叫仿函数;
  • 函数对象本质:

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

  • 函数对象特点:

函数对象在使用时可以有形参、有返回值。
函数对象可以有自己的状态值。
函数对象可以作为函数形参。

使用示例:

#include < iostream >
using namespace std;
class myfunc
{
public:
	myfunc()
	{
		count = 0;
	}
	//求和示例,重载()
	int operator()(int a, int b)
	{
		return a + b;
	}
	//输出示例,count记录函数调用次数
	void operator()(string str)
	{
		count++;
		cout < < str < < endl;
	}
	int count;
};
void print(myfunc& p, string test)
{
	p(test);
}
void test()
{
	//创建一个函数对象
	myfunc p1;
	cout < < "t函数对象形参返回使用示例:" < < endl;
	int ret=p1(10, 20);
	cout < < "ret=" < < ret < < endl;
	cout < < "t仿函数重载示例:" < < endl;
	p1("C++学习--仿函数使用示例!");
	p1("C++学习--仿函数使用示例!");
	p1("C++学习--仿函数使用示例!");
	cout < < "函数调用次数:" < < p1.count < < endl;
	cout < < "t仿函数作为函数形参:" < < endl;
	print(p1, "hello,欢迎学习c++课程");
}
int main()
{
	test();
	system("pause");
}
wKgaomSzow6AR7vbAAV4tKPvhz8386.png

2.谓词

  • 谓词:
    函数对象返回值为bool类型,则称之为谓词;
  • 一元谓词:
    仿函数的形参只有一个;
  • 二元谓词:
    仿函数的形参有两个参数;
#include < iostream >
#include < vector >
#include < algorithm >
using namespace std;
class Check
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
	bool operator()(int a1,int a2)
	{
		return a1 > a2;
	}
};
void test()
{
	vector< int >vtr;
	/*插入数据*/
	for (int i = 0; i < 10; i++)
	{
		vtr.push_back(i);
	}
	cout < < "一元谓词示例:查找vector容器中 >5的值" < < endl;
	/*查找vector容器中 >5的值*/
	vector< int >::iterator ret=find_if(vtr.begin(), vtr.end(), Check());//Check()  ---匿名函数对象
	if (ret ==vtr.end())
	{
		cout < < "未查到到 >5的值!" < < endl;
	}
	else
	{
		cout < < "查找成功,大于5的值为:" < <*ret< ::iterator ptr = vtr.begin(); ptr != vtr.end(); ptr++)
	{
		cout < < *ptr < < " ";
	}
	cout < < endl;
}
int main()
{
	test();
	system("pause");
}
;>
wKgaomSzo36AfA7AAARZcc40veA240.png

3.内建函数对象

  • 内建函数对象:
    STL中提供了一些内建函数对象:算术仿函数、关系仿函数、逻辑仿函数 --头文件

3.1算术运算符

  • 算术仿函数:实现四则运算。

加法:template T plus
减法:template T minus
乘法:template T mutiplies
除法:template T divides
取模:template T modulus
取反:template T negate --正数变负数,负数变正数

注意:其中negate是一元运算(只有一个参数),其余均为二元运算。

#include < iostream >
using namespace std;
#include < functional >
void test()
{
	//negate使用示例:
	negate< int > n;
	cout < < "negate取反示例:" < < n(188) < < endl;
	plus< int > p;
	cout < < "plus加法:" < < p(10, 20) < < endl;
	minus< float >m;
	cout < < "minus减法取绝对值:" < < n(m(10, 20)) < < endl;
	multiplies< float >mt;
	cout < < "multiplies乘法:" < < mt(5, 3.15) < < endl;
	divides< float >d;
	cout < < "divides除法:" < < d(10, 3) < < endl;
	modulus< int >md;
	cout < < "modulus取模:" < < md(10, 3) < < endl;
}
int main()
{
	test();
	system("pause");
}

3.2关系运算符

  • 内建仿函数:关系运算符

大于: templatebool greater

大于等于:templatebool greater_equal

小于: templatebool less

小于等于:templatebool less_equal

等于: templatebool equal_to

不等于: templatebool not_equal_to

#include < iostream >
using namespace std;
#include < functional >
#include < vector >
#include < algorithm >
void print(int val)
{
	cout < < val < < " ";
}
int main()
{
	vector< int > vtr;
	vtr.push_back(10);
	vtr.push_back(40);
	vtr.push_back(30);
	vtr.push_back(60);
	vtr.push_back(6);
	/*sort排序,默认是从小到大,其默认的仿函数即less*/
	sort(vtr.begin(), vtr.end());
	for_each(vtr.begin(), vtr.end(), print);
	cout < < endl;
	/*
		要实现从大小,可以自行实现一个仿函数
		class mycompare
		{
		public:
			bool operator()(int a1,int a2)
			{
				return a1 >a2;
			}
		}
		也可以直接使用STL内建仿函数:greater()
	*/
	sort(vtr.begin(), vtr.end(), greater< int >());
	for_each(vtr.begin(), vtr.end(), print);
	cout < < endl;
	system("pause");

}
wKgZomSzpV-AOdCcAARJXftbJKg672.png

3.3逻辑运算符

  • 内建仿函数--逻辑运算符

逻辑与:templatebool logical_and
逻辑或: templatebool logical_or
逻辑非: templatebool logical_not

#include < iostream >
using namespace std;
#include < vector >
#include < algorithm >
#include < functional >
void print(bool val)
{
	cout < < val < < " ";
}
void test()
{
	vector vtr;
	vtr.push_back(true);
	vtr.push_back(true);
	vtr.push_back(false);
	vtr.push_back(false);
	vectorvtr2;
	vtr2.resize(vtr.size());//设置vtr2的容器大小
	//将vtr容器内容取非放到vtr2中
	transform(vtr.begin(), vtr.end(), vtr2.begin(), logical_not());

	for_each(vtr.begin(), vtr.end(), print);
	cout < < endl;
	for_each(vtr2.begin(), vtr2.end(), print);
	cout < < endl;
}
int main()
{
	test();
	system("pause");
}
wKgaomSzpa-AF8o4AAPxhLsIPiA710.png




审核编辑:汤梓红

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信