函数名 | 功能描述 |
---|---|
sort | 对给定区间全部元素进行排序 |
stable_sort | 对给定区间全部元素进行稳定排序 |
partial_sort | 对给定区间全部元素部分排序 |
partial_sort_copy | 对给定区间复制并排序 |
nth_element | 找出给定区间的某个位置对应的元素 |
is_sorted | 判断一个区间是否已经排好序 |
partition | 使得符合某个条件的元素放在前面 |
stable_partition |
相对稳定的使得符合某个条件的元素放在前面
|
1
2
3
4
|
bool complare(int a,int b)
{
return a>b;
}
|
#include<iostream>
#include<algorithm>
using namespace std;
bool compare(int a,int b)
{
return a>b;
}
int main()
{
int a[10]={9,6,3,8,5,2,7,4,1,0};
for(int i=0;i<10;i++)
cout<<a[i]<<endl;
sort(a,a+10,compare);//在这里就不须要对compare函数传入参数了,
//这是规则
for(int i=0;i<10;i++)
cout<<a[i]<<endl;
return 0;
}
假设本身定义了一个结构体node
struct node
{
int a;
int b;
double c;
}
有一个node类型的数组node arr[100],想对它进行排序:先按a值升序排列,若是a值相同,再按b值降序排列,若是b还相同,就按c降序排列。就能够写这样一个比较函数:
如下是代码片断:
bool cmp(node x,node y)
{
if(x.a!=y.a) return x.a<y.a;
if(x.b!=y.b) return x.b>y.b;
return x.c>y.c;
}
|