包含类的排序列表
C++ Sorting list containing class
我有一个小问题。我目前正在编写一个锦标赛处理代码,我想到了一个最好的方法来保持球队在内存中的顺序将是一个列表。现在,我正在尝试排序包含包含点数记录的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);
相关文章:
- 试图在c++中对数字列表进行排序
- 在STL - C++中按成绩对学生列表进行排序?
- 使用列表 STL 递归进行插入排序
- 列表 iter 不取消引用 使用列表进行插入排序
- 如何在 c++ 中根据第二个元素按降序对列表进行排序
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 如何按键对 std::对列表进行排序?
- 如何通过保持第一个插入的位置来对列表中插入的其余元素进行排序?
- 如何使用cpp编写选择排序算法以降序对元素列表进行排序?
- 使用自定义比较函数在类内进行列表排序
- 列表::排序更改值C
- 对于列表排序,有没有办法使用相同的参数让多个运算符重载?
- 在 c++ 中将数字从列表排序到数组中的气泡排序问题
- 运算符<用于列表排序
- C++列表排序功能
- C++ STL 算法(列表排序)OpenMP/多线程实现
- 列表排序不正确
- 列表排序和结构体向量排序之间的性能差距.c++
- 对抽象数据类型的stl::列表排序
- C++std列表排序使用自定义比较器,该比较器依赖于对象实例的成员变量