包含类的排序列表

C++ Sorting list containing class

本文关键字:列表 排序 包含类      更新时间:2023-10-16

我有一个小问题。我目前正在编写一个锦标赛处理代码,我想到了一个最好的方法来保持球队在内存中的顺序将是一个列表。现在,我正在尝试排序包含包含点数记录的Team类的列表。下面是类声明:

#include "player.h"
#include <string>
class Team {
Player** Gracz;
std::string Name;
int TP, STP;
int Total;
public:
Team();
Team(Player* gracz1, Player* gracz2, Player* gracz3, Player* gracz4, Player*     gracz5, Player* gracz6, std::string name);
~Team();
void SetTeam();
void SetTeam(Player gracz1, Player gracz2, Player gracz3, Player gracz4, Player gracz5, Player gracz6, std::string name);
void SetTP(int tp);
void SetSTP(int stp);
std::string GetTeam();
int GetTotal();
int GetTP();
int GetSTP();
bool operator<(Team& team);
bool operator>(Team& team);
void PrintTeam();
};

程序代码如下:

#include <iostream>
#include "player.h"
#include "team.h"
#include <list>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
Player *p;
Team *t1, *t2, *t3, *t4;
list<Team> x;
list<Team>::iterator it;
p=new Player("a","a","a");
t1=new Team(p,p,p,p,p,p,"A");
t2=new Team(p,p,p,p,p,p,"B");
t3=new Team(p,p,p,p,p,p,"C");
t4=new Team(p,p,p,p,p,p,"D");
x.push_back(*t1);
x.push_back(*t2);
x.push_back(*t3);
x.push_back(*t4);
cout<<"Turniej: "<<endl;
for(it=x.begin();it!=x.end();++it)
    cout<<" "<<(*it).GetTeam();
cout<<endl;
t1->SetTP(15);
t2->SetTP(4);
t3->SetTP(8);
t4->SetTP(8);
t3->SetSTP(15);
t4->SetSTP(65);
x.sort(); 
cout<<"Turniej: "<<endl;
for(it=x.begin();it!=x.end();++it)
    cout<<" "<<(*it).GetTeam();
cout<<endl;
return 0;
}

所以我想先按tp排序,然后按stp排序,它包含在重载操作符

复制对象,并将其副本压入容器中:

x.push_back(*t1);
/* the same for others */

这里修改了原始对象,但是容器中的副本没有改变:

t1->SetTP(15);