C++学习笔记(四)—— 使用sort()对结构体排序

bool comp(example x, example y)
{
    return x.a<y.a;
    //<升序,>降序,x.a以a排序,x.b以b排序
}

struct example
{
    int a,b;
}ex[n];

int main(void)
{
//...
    sort(ex, ex+n, comp);
//...
}

猜你喜欢

转载自www.cnblogs.com/esperanza/p/12165559.html