以下哪个自定义比较器可以正确实现C++ STL std::sort的多条件排序需求?

dv813112 单选题 共 个子题

现有如下C++代码,意图对存储学生信息的vector按成绩降序排序,成绩相同时按姓名字典序升序排列:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

struct Student {
    std::string name;
    int score;
};

int main() {
    std::vector<Student> stu = {{"Bob", 90}, {"Alice", 90}, {"Charlie", 85}};
    std::sort(stu.begin(), stu.end(), 【此处填入比较器】);
    return 0;
}

以下哪个选项作为比较器可以实现预期效果?

A
[](const Student& a, const Student& b) {
    return a.score > b.score || a.name < b.name;
}
B
[](const Student& a, const Student& b) {
    return a.score > b.score || (a.score == b.score && a.name < b.name);
}
C
[](const Student& a, const Student& b) {
    return a.score < b.score || a.name < b.name;
}
D
[](const Student& a, const Student& b) {
    return a.score > b.score && a.name < b.name;
}
正在加载题目数据…
已答 /
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析
讨论
发布
登录后参与讨论 立即登录
发布讨论需先绑定并验证手机号 去绑定手机号
加载中…
我
我
回复 @
回复 @
发送
还没有人讨论,来发表第一条吧