预定义函数对象

#include <iostream>    
#include<string>    
#include <vector>    
#include <list>    
#include <set>    
#include <map>    
#include <algorithm>    
#include <functional>  
using namespace std; 

//plus 预定义函数对象 能实现不能类型数据的+运算
// 实现了算法和数据类型的分离 ===》通过函数对象技术实现的
/*
template<class _Ty>
struct plus
: public binary_function<_Ty, _Ty, _Ty>
{	// functor for operator+
_Ty operator()(const _Ty& _Left, const _Ty& _Right) const
{	// apply operator+ to operands
return (_Left + _Right);
}
};
*/
void display()
{
	
	plus<int> intadd;
	int x = 10l;
	int y = 200;
	int z = intadd(x,y);
	cout<<z<<endl;

	plus<string> stringadd;
	string s1 = "aaaa";
	string s2 = "bbbb";
	string s3 = stringadd(s1,s2);
	cout<<s3<<endl;
	cout<<"---------------"<<endl;

	/*
	template<class _RanIt,
	class _Pr> inline
	void sort(_RanIt _First, _RanIt _Last, _Pr _Pred)
	{	// order [_First, _Last), using _Pred
	_DEBUG_RANGE(_First, _Last);
	_DEBUG_POINTER(_Pred);
	_Sort(_Unchecked(_First), _Unchecked(_Last), _Last - _First, _Pred);
	}
	*/
	vector<string> v;
	v.push_back("dddd");
	v.push_back("aaaa");
	v.push_back("cccc");
	v.push_back("cccc");
	v.push_back("bbbb");

	sort(v.begin(),v.end(),greater<string>());

	for (vector<string>::iterator it  = v.begin();it!=v.end();it++)
	{
		cout<<*it<<endl;
	}
	
	
	//统计和字符串相等的个数
	//equal_to<string> 有两个参数 left参数来自容器,right参数来自sc;
	//bind2nd函数适配器 把预定义函数对象和第二个参数进行绑定
	string sc = "cccc";

	int num = count_if(v.begin(),v.end(),bind2nd(equal_to<string>(),sc));

	cout<<num<<endl;
}
int main()
{
	display();
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值