今天写题目又遇到了给结构体排序,于是百度了一波,总结一下:

struct stu {
    int id;
    string name;
};

bool cmp(stu s1, stu s2)
{
    return s1.id > s2.id;
}

vector<stu> s;
......
sort(s.begin(), s.end(), cmp);

如上,通过cmp函数来进行比较就OK~

Last modification:March 29, 2019
If you think my article is useful to you, please feel free to appreciate