如何使用字符串对对象矢量进行排序?

时间:2014-08-31 16:28:02

标签: c++

我有一个这样的课程:

class student
{
    public:
    string name;
    int age;
    int rollnum;
    int year;
    string father;
    string mother;
    string category;
    string region;
    char sex;
    string branch;
    int semester;
};

我已经创建了这样的班级学生的矢量(矢量j1),现在我想根据他们的名字对它们进行排序。

我该怎么做?

PS:我做到了。

student lessthan
{
    bool operator() (const student& h1, const student& h2)
    {
        return (h1.name < h2.name);
    }
};

然后做了这个,

sort(j1.begin(),j1.end(),lessthan());

但是这显示了在bool之前说预期的主要表达的错误。

1 个答案:

答案 0 :(得分:3)

sort( j1.begin(),
     j1.end(),
     [](const student& s1, const student& s2){ return s1.name < s2.name;} );