Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ (C+;+;)排序()中第三个参数的决策结构_C++_Sorting - Fatal编程技术网

C++ (C+;+;)排序()中第三个参数的决策结构

C++ (C+;+;)排序()中第三个参数的决策结构,c++,sorting,C++,Sorting,我正在研究一个问题的简单解决方案,我想对通过以下方式定义的对象向量进行排序: class Person { public: string name_; double age_; }; 我试图修改sort()的第三个参数,以便可以按年龄对向量进行排序,但当两个年龄相同时,它会在该年龄内按字母顺序对向量进行排序 vector<Person> people; //code to populate vector sort(people.begin(), people.e

我正在研究一个问题的简单解决方案,我想对通过以下方式定义的对象向量进行排序:

class Person {
  public:
    string name_;
    double age_;
};
我试图修改sort()的第三个参数,以便可以按年龄对向量进行排序,但当两个年龄相同时,它会在该年龄内按字母顺序对向量进行排序

vector<Person> people;
//code to populate vector
sort(people.begin(), people.end(), sort_decision);
矢量人;
//填充向量的代码
排序(people.begin()、people.end()、sort\u decision);
我不确定是否可以将决策结构添加到我拥有的排序决策代码中,大致如下所示:

inline bool sort_decision(Person const& lhs, Person const& rhs) {
    if (lhs.age_ == rhs.age_)
        return lhs.age_ < rhs.age_ && lhs.name_ < rhs.name_;
    else
        return lhs.age_> rhs.age_;
}
内联布尔排序决策(人员常量和lhs、人员常量和rhs){
如果(lhs.age==rhs.age)
返回lhs.agerhs.age;
}

谢谢你的帮助

排序决策应该是这样的

inline bool sort_decision(Person const& lhs, Person const& rhs) {
    if (lhs.age_ == rhs.age_)
        lhs.name_ < rhs.name_;
    else
        return lhs.age_> rhs.age_;
}
内联布尔排序决策(人员常量和lhs、人员常量和rhs){
如果(lhs.age==rhs.age)
lhs.name urhs.age;
}

因为如果
if(lhs.age<=rhs.age
为真,
返回lhs.age<
将始终为假,如果年龄相同,您的比较器将返回假。

想想
返回lhs.age在
lhs.age.==rhs.age.
时执行。问问自己,在这种情况下,lhs.age的结果是什么。