本站第一个问题,我想按获得的积分排序,球队排名,有错误但是做空不行。 我需要你的帮助来解决这个问题
团队.h
class Team
{
private:
std::string t_name;
int t_rank;
public:
int t_points;
Team(std::string name);
~Team();
void win();
void show(std::ostream &Flux)const;
};
std::ostream& operator<<(std::ostream &Flux, Team const &B);
bool operator<(Team const& A,Team const& B);
团队.cpp
Team::Team(string name): t_name(name), t_points(0) {}
void Team::win()
{
t_points+=3;
}
void Team::show(std::ostream &Flux)const
{
Flux << t_name;
}
std::ostream& operator<<(std::ostream &Flux, Team const &B)
{
B.show(Flux);
return Flux;
}
bool operator<(Team const& A,Team const& B){
return A.t_points < B.t_points;
}
主要.cpp
int main()
{
vector <Team*> Schedule;
Schedule.push_back(new Team("Celtics")); //0
Schedule.push_back(new Team("Nets"));//1
Schedule.push_back(new Team("Bulls"));//2
Schedule[1]->win();
Schedule[1]->win();
Schedule[2]->win();
std::sort(Schedule.begin(), Schedule.end());
for(int i(0); i<Schedule.size(); i++)
{
cout << i << " - " << *Schedule[i] << endl;
}
return 0;
}
我已经编辑了我的文件以添加您提供的信息,但仍然无法解决我的问题,所以我重新发布所有内容并等待更多帮助
最佳答案
自 vector<Team*>
存储一个你需要重载 operator<(const Team*, const Team*) operator
的指针像这样:
bool operator<(const Team* left, const Team* right) {
return left->whatever < right->whatever;
}
关于c++ - 对象的排序 vector 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46119957/