我可以用g++编译代码,cin也不错。但是,按 Enter 后我没有得到任何输出,我可以继续输入单词。有什么问题?
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main() {
map<string, size_t> word_count;
string word;
while (cin>>word) {
++word_count[word];
}
for (auto &w : word_count) {
cout<<w.first<<" occurs "<<w.second<<" times"<<endl;
}
return 0;
}
最佳答案
只要您输入的是有效字符串,
while(cin>>word)
就会循环。空字符串仍然是有效字符串,因此循环永远不会结束。
关于c++ - 为什么代码没有cout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43394131/