[英]Function that scan through file and set string and integer parameter to finding the 5 largest numbers in the text file C++
我做了一个 function 设置 integer 和字符串参数来分别找到最高的 5 个分数和 5 个玩家的名字。
通过排序 function,我想出打印 5 个最高分:
std::sort(highscore, highscore + 5, greater<int>());
但球员的名字保持不变。
例如:
输入:
Mark
500
Sarah
200
Stan
250
Michelle
1000
Franc
4900
Output:(如你所见,名字还是一样的)
Mark
4900
Sarah
1000
Stan
500
Michelle
250
Franc
200
通过什么程序,我可以分别按分数顺序交换名称?
使用自定义类型
struct player {
std::string name;
int score;
};
然后根据分数对std::vector<player>
进行排序,例如通过
auto cmp_score = [](const player& a, const player& b) { return a.score < b.score; };
std::sort(players.begin(),players.end(),cmp_score);
如评论中所述,使用 5 个元素的最小堆可避免将所有播放器从 memory 中的文件中保留。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.