该程序应该存储标准输入流中给出的每个单词并计算它们的出现次数。结果应该在之后按顺序打印,然后是它们的计数。据我所知,该程序以其他方式工作,但字符串被打印为字符的 ASCII 值而不是字符本身。怎么了?
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
#include <algorithm>
std::string get_word();
int main()
{
std::vector<std::string> words;
std::string word;
while (std::cin.good()) {
word = get_word();
if (word.size() > 0)
words.push_back(word);
}
std::sort(words.begin(), words.end());
unsigned n, i = 0;
while (i < words.size()) {
word = words[i];
n = 1;
while (++i < words.size() and words[i] == word)
++n;
std::cout << word << ' ' << n << std::endl;
}
}
std::string get_word()
{
while (std::cin.good() and !std::isalpha(std::cin.peek()))
std::cin.get();
std::stringstream builder;
while (std::cin.good() and std::isalpha(std::cin.peek()))
builder << std::cin.get();
return builder.str();
}