C++ 如何使用字符串对对象向量进行排序?
我有一门课是这样的:C++ 如何使用字符串对对象向量进行排序?,c++,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; }; 我已经创建了一个此类学生的向量(向
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);
}
};
但是这显示了一个错误,在bool之前说expected primary expression。sort(j1.begin(),
sort( j1.begin(),
j1.end(),
[](const student& s1, const student& s2){ return s1.name < s2.name;} );
j1.end(),
[](constudent&s1,constudent&s2){返回s1.name
排序(j1.begin(),
j1.end(),
[](constudent&s1,constudent&s2){返回s1.name
排序(j1.begin(),
j1.end(),
[](constudent&s1,constudent&s2){返回s1.name
排序(j1.begin(),
j1.end(),
[](constudent&s1,constudent&s2){返回s1.name
lessthan
应该是类
/结构
。关于student
?student
的打字应该是struct
charsex代码>这不完全是类型安全的。它应该允许'M'
和'F'
。或者'm'
和'f'
?或者两者都有?如果有人意外地分配了'a'
或'b'
,或123或255或0,该怎么办?您至少应该使用枚举。lessthan
应该是类
/结构
。关于student
?student
的打字应该是struct
charsex代码>这不完全是类型安全的。它应该允许'M'
和'F'
。或者'm'
和'f'
?或者两者都有?如果有人意外地分配了'a'
或'b'
,或123或255或0,该怎么办?您至少应该使用枚举。lessthan
应该是类
/结构
。关于student
?student
的打字应该是struct
charsex代码>这不完全是类型安全的。它应该允许'M'
和'F'
。或者'm'
和'f'
?或者两者都有?如果有人意外地分配了'a'
或'b'
,或123或255或0,该怎么办?您至少应该使用枚举。lessthan
应该是类
/结构
。关于student
?student
的打字应该是struct
charsex代码>这不完全是类型安全的。它应该允许'M'
和'F'
。或者'm'
和'f'
?或者两者都有?如果有人意外地分配了'a'
或'b'
,或123或255或0,该怎么办?您至少应该使用枚举。begin(j1)
而不是j1.begin()
更为惯用。可能需要解释这是lambda。begin(j1)
而不是j1.begin()
更为惯用。可能需要解释这是lambda。begin(j1)
而不是j1.begin()
更为惯用。可能需要解释这是一个lambda。begin(j1)
而不是j1.begin()
更为惯用。可能需要解释这是一个lambda。
sort( j1.begin(),
j1.end(),
[](const student& s1, const student& s2){ return s1.name < s2.name;} );