我创建了一个小高分系统,可以保存到文件中,然后根据请求读取/加载。
但是,我正在加载到字符串向量中,因此创建了一个字符串构建器来添加它们并按顺序显示。
但是,我意识到这是一种不好的做法,并且不确定如何获得我想要的结果,即拥有一个可以按分数(降序)顺序以及相应名称排序的数据结构。
理论上我想做的是<vector<int, string> theScore. 如果有人能指出我正确的方向,我将不胜感激。
这是我所拥有的:
vector<string> HighScore::loadScore()
{
ifstream loadFile("scorefile.txt");
string name;
int score;
vector<string> theScore;
string builder;
if (loadFile.is_open())
{
    while (!loadFile.eof())     
    {
        loadFile >> name >> score;
        builder = to_string(score) + "\t";
            builder = builder + name;   
            //Add all entries to vector List
                theScore.push_back(builder);                                
        }   
        //Sort all entries in score to descending order (Reverse)
        std::sort(theScore.rbegin(), theScore.rbegin() +theScore.size());   
    }
    return theScore;
}