栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【C++】内建函数对象

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【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;
}
//总结:逻辑仿函数实际应用较少,了解即可
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/691522.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号