第十一章关联容器

时间:2021-05-28 11:30:46   收藏:0   阅读:10

习题

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
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!