sort是STL中的函数
1, 最常见的用法是将数组按照递增顺序排序,
//a是vector数组
sort(a.begin(), a.end());
2,一般情况下sort为升序,若需要将数组进行降序,则需要改写compare函数
bool compare(const int &a, const int &b)
{
return a > b; //如此即为降序,return a < b 则为升序
} sort(a.begin(), a.end(), compare);
3,上面的情况可以扩展到结构体
typedef struct Node
{
int id;
int val;
}Node;
Node node[100];
/*
*对node进行赋值
*/
//对结构体一级排序
bool compare(const Node &a, const Node &b)
{
return a.id < b.id; //针对id 进行升序排序
}
//对结构体进行二级排序
bool compare(const Node &a, const Node &b)
{
if(a.id!=b.id)
return a.id < b.id;
else
return a.val < b.val;
}
sort(node, node+100, compare);