c++ - 在 C++ 中查找字符串中的重复单词时输出错误
问题描述
你能告诉我为什么 1 出现在我的输出中,而我正在使用映射跟踪给定字符串中的单词和出现吗?
string x="Hello World Hello World";
//getline(cin,x);
map<string,int> store;
stringstream s(x);
do
{
string y;
s>>y;
if(store.find(y)!=store.end())
{
auto it=store.find(y);
it->second++;
}
else
store.insert(pair<string,int>(y,1));
}while(s);
map<string, int>::iterator itr; //common for all maps
for(itr=store.begin();itr!=store.end();++itr)
cout<<itr->first<<" "<<itr->second<<endl;
输出:
1
Hello 2
World 2
解决方案
我通过使用 y.length()>0 纠正了条件的其他部分,现在它似乎工作正常。
推荐阅读
- python - 当我使用正常精度和 K 折交叉验证运行决策树算法时得到两个完全不同的结果
- html - iOS Safari 会在不需要时要求在网站上自动登录
- python - 当我最大化我的窗口时,我的小部件没有被替换
- amazon-web-services - 调用 MediaConvert SDK 来描述端点超时的 Lambda 函数
- javascript - 在传递另一个 div 后显示 div 不显示阻止,然后使用 onclick 再次显示无
- javascript - 失败:javascript 错误:循环引用
- kubernetes - kubernetes 生成\创建资源配置
- javascript - U2F - 类型错误 | 无法注册密钥
- sql - 在 R 中读取 SQL 查询时使用带日期的 Where 子句
- javascript - 基于浏览器时间的 CSS