005内建仿函数_关系仿函数

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

/*
 *功能描述:
实现关系对比
仿函数原型:
template<class T> bool equal_to<T> //等于
template<class T> bool not_equal_to<T> //不等于
template<class T> bool greater<T> //大于
template<class T> bool greater_equal<T> //大于等于
template<class T> bool less<T> //小于
template<class T> bool less_equal<T> //小于等于
 */
class MyCompare
{
public:
	bool operator()(int v1,int v2)
	{
		return v1>v2;
	}
};
void test01()
{
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	for(vector<int>::iterator it=v1.begin();it!=v1.end();it++)
	{
		cout<<*it<<endl;
	}
	cout<<endl;

	//实现仿函数
	//sort(v1.begin(),v1.end(),MyCompare());
	//stl内建的仿函数,大于自己定义的
	sort(v1.begin(),v1.end(),greater<int>());
	for(vector<int>::iterator it=v1.begin();it!=v1.end();it++)
	{
		cout<<*it<<endl;
	}
	cout<<endl;
}
using namespace std;
int main(void)
{
	test01();
	system("pause");
	return 0;
}
/*
 * 10
20
30

30
20
10

请按任意键继续. . .

 */

猜你喜欢

转载自blog.csdn.net/baixiaolong1993/article/details/89641752