首页 > 其他 > 详细

第十一章关联容器

时间:2021-05-28 11:30:46      阅读:9      评论:0      收藏:0      [点我收藏+]

习题

11.4编写你自己的单词计数程序,扩展你的程序,忽略大小写和标点。例如,"example."、"example,"和"Example"应该递增相同的计数器。

#include <string>
#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    map<string, int> word_count;
    string tmp;
    while (cin >> tmp){
        for (char &ch : tmp)
            ch = tolower(ch);
        tmp.erase(remove_if(tmp.begin(), tmp.end(), ::ispunct), tmp.end());
        word_count[tmp] += 1;
    }
    for (const auto& elem : word_count)
        std::cout << elem.first << " : " << elem.second << endl;
    return 0;
}

 

第十一章关联容器

原文:https://www.cnblogs.com/11ys/p/14820981.html

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