STL 仿函数的分类与运用

1 篇文章 0 订阅
六个算数运算符

plus<type> //加法
minus<type> //减法
negetive<type> //否定
multiplies<type> //乘法
divides<type> //除法
modules<type> //取模

代码示例:
#include <iostream>
#include <numeric>
#include <vector> 
#include <functional> 
using namespace std;
int main()
    {
    	int ia[] = { 1,2,3,4,5 };
    	vector<int> iv(ia, ia + 5);
    	//120
    	cout << accumulate(iv.begin(), iv.end(), 1, multiplies<int>()) << endl;
    	//15
    	cout << multiplies<int>()(3, 5) << endl;
     
    	modulus<int>  modulusObj;
    	cout << modulusObj(3, 5) << endl; // 3 
    	system("pause");
    	return 0;
    }
六个关系运算类仿函数

less<type> //小于
less_equal<type> //小于等于
greater<type> //大于
greater_equal<type> //大于等于
equal_to<type> //等于
not_equal<type> //不等于

代码示例:
#include <iostream>
#include <algorithm>
#include<functional>
#include <vector> 
 
using namespace std;

template <class T>
class display
{
public:
	void operator()(const T &x)
	{
		cout << x << " ";
	}
};
int main()
{
	int ia[] = { 1,5,4,3,2 };
	vector<int> iv(ia, ia + 5);
	sort(iv.begin(), iv.end(), greater<int>());
	for_each(iv.begin(), iv.end(), display<int>());
	system("pause");
	return 0;
}
三个逻辑运算符

logic_and<type> //逻辑于
logic_and<type> //逻辑或
logic_not<type> //逻辑否

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值