<分区>
我有一个包含成员变量 name 的 Person 对象数组。我正在尝试使用重载比较运算符按字母顺序排列它们,该运算符使用 string::compare 来比较对象中的字符串。
bool Person::operator==(Person p) {
if (name.compare(p.name) == 0) {
return true;
}
else {
return false;
}
}
这行得通并给出了正确的结果,但我不知道如何使用它来按字母顺序排列数组中的名称。
我环顾四周,看到关于 string::compare 的文档说
relation between compared string and comparing string
- 0 They compare equal
- <0 Either the value of the first character that does not match is lower in > the compared string, or all compared characters match but the compared string > is shorter.
- ,>0 Either the value of the first character that does not match is greater >in the compared string, or all compared characters match but the compared string is longer.
尽管如此,我无法找到解决我的问题的方法。
我正在努力做的事情是否走在正确的轨道上,或者是否有更好的方法?