基本上,我想从文件中读取高分,并检查用户是否在记分板上获得了足够的分数。我试着这样做:
string initials[10];
int scores[10];
//load txt
ifstream highscores("highscores.txt");
if(highscores.is_open())
{
while(highscores.good())
{
for(int x=0;x<10;x++)
{
getline(highscores,initials[x],' ');
highscores >> scores[x];
}
}
}首字母的长度只有3个字符,所以我可以实现一个2dim。数组,但我想用字符串来尝试它。它显示我生成了一个大小为10的字符串,我该如何编写它才能使用10个数组而不是1个数组?(我知道我可以从array1中将它们命名为10个数组。到10,循环遍历它们听起来要好得多。高分文件只是一组10个缩写的AAA,BBB等和一些分数。
Highscores.txt示例:
AAA 5000
BBB 4000
CCC 3000发布于 2011-12-10 21:00:23
使用std::map保存首字母和相关分数。例如:
int main()
{
// Map is keyed by initials.
std::map<std::string, int> scores;
std::ifstream in("highscores.txt");
if (in.is_open())
{
for (;;)
{
std::string line;
std::getline(in, line);
if (!in.good())
{
break;
}
const size_t space_idx = line.find(' ');
if (std::string::npos != space_idx)
{
// The initials are everthing before the space.
// Score everything after the space.
scores[line.substr(0, space_idx)] =
atoi(line.substr(space_idx + 1).c_str());
}
}
in.close();
}
// Check who has achieved required score.
for (std::map<std::string, int>::iterator i = scores.begin();
i != scores.end();
i++)
{
if (i->second > 3500)
{
std::cout << i->first << "\n";
}
}
return 0;
}发布于 2011-12-11 01:53:54
有两个任务:
如果用户分数足够高,
//添加新分数(score => name pair);弹出最低分返回模板typename Map::value_type add_score_if(Map& score,typename Map::value_type new_score) { scores.insert(new_score);//弹出最低分auto it = scores.begin();typename Map::value_type lowerest(* it );scores.erase(it);return lowerest;}
add_score_if()弹出最低的分数,因此如果new_score不够高,它将不会留在分数表中,即在这种情况下,scores的内容在add_score_if()之前/之后是相同的。
//从输入流加载分数(分数和名称对) //其中每行是:名称分数//返回是否所有分数都已加载模板< Map> Istream,类映射bool load_scores(Istream& in,=> & score) { std::string name;int score;while (in >> name >> score ) scores.insert(std::make_pair (score,name));return in.eof();//XXX忽略eof处的错误}
程序
#include <iostream>
#include <map>
template<class Map> void
dump_scores(std::ostream& out, const Map& scores) {
for (auto it = scores.rbegin(); it != scores.rend(); ++it)
out << it->second << ' ' << it->first << '\n';
}
int main() {
// load scores
std::multimap<int, std::string> scores;
if (! load_scores(std::cin, scores)) {
std::cerr << "error: not all scores have been loaded\n";
return 1;
}
std::cout << "Before:\n";
dump_scores(std::cout, scores);
// add new score
add_score_if(scores, std::make_pair(4000, "AAA"));
std::cout << "\nAfter:\n";
dump_scores(std::cout, scores);
}示例
$ g++ -std=c++0x *.cc && printf "AAA 5000\nCCC 3000\nBBB 4000" | ./a.out
Before:
AAA 5000
BBB 4000
CCC 3000
After:
AAA 5000
AAA 4000
BBB 4000发布于 2011-12-10 21:16:13
重读后得到您的问题:)
正如您所提到的,
string str[10]不会创建10个字符串的数组,因为字符串是在堆上创建的,而不是在堆栈上创建的。如果你想创建一个字符串数组来放入你的数据字符,它应该是char * name[10];,并且每次你读取一行并得到前3个字符时,你就执行new char[3] (你必须把它存储在to..then中,对吧??)。
另外,为了更有效率并依赖于您的数据,您可以创建一个char arr[30]并自己进行3个字节的对齐以读取所有内容。
现在,您可以通过使用STL容器使您的工作变得更容易。
vector < map < string,int > > arr; arr.reserve(10);
这样做的好处很多: 1)不需要你来做内存管理。
2)使用迭代器遍历。
3)如果你将它与我的第一种方法进行比较,在性能上也不会有太大的差异。
https://stackoverflow.com/questions/8456581
复制相似问题