家长班:
Character
子类:
Elf
Human
我有一个变量是
int myScore;
string myType;
所以我所做的是按 myType 排序,然后按升序排序
意味着如果我有这样的记录
[1] Human, 99
[2] Elf, 91
[3] Elf, 99
[4] Human, 99
如果排序它将是
[1] Human, 90
[2] Human, 99
[3] Elf, 91
[4] Elf, 99
之前听说过可以将2排序合并为1的merge函数。
但是我该如何使用它。
我现在所做的是
在字符.cpp
struct sort_by_score
{
static bool operator()(Character* x, Character* y)
{
return x->getScore() < y->getScore();
}
};
在 main.cpp
我做了这个
int main()
{
Character *chara[100];
vector<Character*> sortVector;
//some input of value here.. assuming now got 4 pointers to object, 2 human 2 elf.
sortVector.clear();
sortVector.assign(chara,chara + characterCounter);
//here i got question on how to sort by Human Then Elf
//2nd sort is sort by score
sort(sortVector.begin(), sortVector.end(), sort_by_score());
for (int i=0;i<characterCounter;i++)
{
cout << sortVector.toDisplay() << endl;
}
return 0;
}
感谢大家的帮助!!