常用排序算法:
sort
谓词----bool
代码语言:javascript复制#include<iostream>
using namespace std;
#include<deque>
#include<algorithm>
//sort算法
void p(int val)
{
cout << val << " ";
}
class p1 {
public:
void operator()(int val)
{
cout << val << " ";
}
};
//改变排序规则
class s1 {
public:
bool operator()(int val, int val1)
{
//只有当第一个参数大于第二个参数时才是降序--从大到小排列
return val> val1;
}
};
bool s(int val, int val1)
{
return val > val1;
}
void test01()
{
deque<int> d = { 2,5,6,8,1,9,4 };
//遍历:for_each
for_each(d.begin(), d.end(), p);
//利用sort排序
//默认升序
cout << "n默认排序:";
sort(d.begin(), d.end());
for_each(d.begin(), d.end(), p1());
cout << "n降序排列:";
//sort(d.begin(), d.end(),s1());
//sort(d.begin(), d.end(), s);
//方式3:内建函数对象----vs高级版本可不用包含头文件functional
sort(d.begin(), d.end(), greater<int>());
for_each(d.begin(), d.end(), p1());
}
int main()
{
test01();
cout << endl;
system("pause");
return 0;
}