首页 > 其他 > 详细

使用关联容器

时间:2020-03-15 13:42:25      阅读:61      评论:0      收藏:0      [点我收藏+]

使用 map

使用 map 为单词计数:

map<string, size_t>  word_count;
string word;
while (cin >> word)
++word_count[word];

for (const auto & w : word_count)
    cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times" : " time") << endl;

使用 set

接上面的例子,只统计不在 set 中的单词:

map<string, size_t>  word_count;
set<string> exclude{"the","a","an","or","and"};
string word;
while (cin >> word)
{
    if(exclude.find(word) == exclude.end())
    ++word_count[word];
}   

for (const auto & w : word_count)
    cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times" : " time") << endl;

使用关联容器

原文:https://www.cnblogs.com/xiaojianliu/p/12496898.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!