【C++】内建函数对象

【C++】内建函数对象,第1张

【C++】内建函数对象


#include
using namespace std;
#include//内建函数对象头文件
//内建函数对象 算数仿函数

//negate 一元仿函数 去反仿函数

void test01()
{
	negaten;

	cout << n(50) << endl;
}

//plus 二元仿函数 加法
void test02()
{
	plusp;

	cout << p(10, 20) << endl;
}

int main() {
	//test01();
	test02();
	system("pause");

	return 0;
}
//总结:使用内建函数对象时,需要引入头文件#include

#include
using namespace std;
#include
#include
#include
//内建函数对象--关系仿函数
//大于greater

class MyCompare
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 < v2;
	}
};

void test01()
{
	vectorv;

	v.push_back(10);
	v.push_back(30);
	v.push_back(40);
	v.push_back(20);
	v.push_back(50);

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

	//降序
	//sort(v.begin(), v.end(), MyCompare());
	//greater()内建函数对象
	sort(v.begin(), v.end(), greater());
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}	

int main() {
	test01();
	system("pause");

	return 0;
}
//总结:关系仿函数中最常用的就是greater<>大于

#include
using namespace std;
#include
#include
#include

//内建函数对象 逻辑仿函数
//逻辑非 logical_not

void test01()
{
	vectorv;
	v.push_back(true);
	v.push_back(false);
	v.push_back(true);
	v.push_back(false);

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

	//利用逻辑非 将容器v搬运到容器v2中,并执行取反操作
	vectorv2;
	v2.resize(v.size());

	transform(v.begin(), v.end(), v2.begin(), logical_not());
	
	for (vector::iterator it = v2.begin(); it != v2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

}

int main() {
	test01();
	system("pause");

	return 0;
}
//总结:逻辑仿函数实际应用较少,了解即可

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5692007.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存