#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;
}
预定义函数对象
最新推荐文章于 2022-04-06 21:52:23 发布