004内建的仿函数_算术仿函数

/*
 * 概念:
STL内建了一些函数对象
分类:
算术仿函数
关系仿函数
逻辑仿函数
用法:
这些仿函数所产生的对象,用法和一般函数完全相同
使用内建函数对象,需要引入头文件 #include<functional>
.3.2 算术仿函数
功能描述:
实现四则运算
其中negate是一元运算,其他都是二元运算
仿函数原型:
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> //取反(负数)仿函数
 */
#include<iostream>
#include <set>
#include <functional>
using namespace std;

//取反
void test01()
{
	negate<int> n;
	cout<<n(50)<<endl;
}

void test02()
{
	plus<int> p;
	cout<<p(10,20)<<endl;
}

using namespace std;
int main(void)
{
	test01();
	test02();
	system("pause");
	return 0;
}
//总结:使用内建函数对象时,需要引入头文件 #include <functional>
/*
 -50
30
请按任意键继续. . .


 */

猜你喜欢

转载自blog.csdn.net/baixiaolong1993/article/details/89641352
今日推荐