sort:
包含头文件 algorithm
默认排序方式为升序,但可根据自定义comp函数来达到其他要求。
1、用法
数组:
int a[100];
sort(a, a+100);
vector:
vector<int> v;
sort(v.begin(), v.end());
2、降序排序
int a[100];
bool comp(const int a,const int b)
{
return a>b;
}
sort(a, a+100, comp);
3、对结构体内某元素排序
struct Node{
int x;
int y;
}node[100];
bool comp(const Node a,const Node b){
return a.a < b.a;
}
sort(node, node+100, comp);
4、对pair排序
pair<int, int> p[100];
bool comp(const pair<int, int> &a, const pair<int, int> &b){
return a.first < b.first;
}
sort(p, p+100, comp);
不进行typedef定义pair,comp参数里要用pair< int, int >