forked from jzplp/Cpp-Primer-Answer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.20.cpp
32 lines (27 loc) · 754 Bytes
/
11.20.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<iterator>
#include<utility>
int main()
{
std::map<std::string, size_t> word_count;
std::string word;
while(std::cin >> word)
{
for(char &c : word)
c = tolower(c);
std::string word_clean;
std::copy_if(word.begin(), word.end(), std::back_inserter(word_clean), [](char c){ return isalpha(c); });
if(word_clean == "")
continue;
std::pair<std::map<std::string, size_t>::iterator, bool> pib = word_count.insert({word_clean, 1});
if(pib.second != true)
++(pib.first->second);
}
for(const auto &w : word_count)
std::cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times" : " time") << std::endl;
return 0;
}