function: 实现关系对比 仿函数原型:
代码语言:javascript复制#include<iostream>
using namespace std;
#include<functional>
#include<vector>
#include<algorithm>
void p(const vector<int>& v)
{
for (int i = 0; i < v.size(); i )
{
cout << v[i] << " ";
}
cout << endl;
}
class compare {
public:
bool operator()(int v1, int v2) const
{
return v1 > v2;
}
};
void test()
{
//1.关系仿函数
vector<int> v = { 3,25,67,4,2,7,34,56 };
//未打印输出前
cout << "未排序:" << endl;
p(v);
cout << "----------------------------" << endl;
//sort----升序
sort(v.begin(), v.end());
cout << "升序:" << endl;
p(v);
cout << "--------------------------------" << endl;
//改变排序规则
//第一种方法:
//sort(v.begin(), v.end(),compare());
//第二种:
sort(v.begin(), v.end(), greater<int>());
cout << "降序:" << endl;
p(v);
cout << "--------------------------------" << endl;
}
int main()
{
test();
system("pause");
return 0;
}
总结:用的比较多的是大于