我对C++相当陌生,我正在尝试实现一个高效的排序算法。我想这是四个球员工作,但目前我正在测试我的程序为2个球员。每个球员都有一个进球,一个球和一个ID。例如,如果玩家1比玩家2有更多的进球,玩家2就是胜者。如果玩家1和玩家2的进球数相同,那么球数最多的玩家就是胜者。如果玩家1和玩家2的进球数和球数相同,那么id最高的玩家获胜。编译代码时,会出现错误
谁能帮帮我吗?提前致谢
#include <iostream>
#include <string>
#include <algorithm>
struct player {
int goals = 0;
int balls = 0;
int id = 0;
};
int main()
{
std::vector<player> players(2);
players[0].goals = 3;
players[0].balls = 3;
players[0].id = 0;
players[1].goals = 4;
players[1].balls = 3;
players[1].id = 1;
std::sort(players.begin(),players.end(),[](const player& a,const player& b) {
if(a < b)
{
std::cout<<"player_1 wins"<<std::endl;
}
else
{
std::cout<<"player_2 wins"<<std::endl;
}
return true;
});
}
结构默认情况下没有比较运算符,所以你必须自己实现(重载)。
struct player {
int goals = 0;
int balls = 0;
int id = 0;
// operator overload to define < operator
bool operator<(const player& other) const {
// return true if this object is "less than" other and false otherwise
}
};