我創建了一個小高分系統,保存到文件,然後根據請求讀取/加載。如何返回一個int和一個字符串?
但是,我加載到一個字符串的向量中,所以創建了一個字符串生成器來添加它們並按順序顯示。
但是,我意識到這是一個糟糕的做法,我不確定如何獲得我想要的結果,這將有一個數據結構,可以按照分數(降序)排序相應的名字。
我想在理論上做的是<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;
}
創建一個具有Siring和int變量的類an d然後返回對象。 – 2013-04-10 08:26:31