forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex11_20.cpp
More file actions
28 lines (24 loc) · 708 Bytes
/
Copy pathex11_20.cpp
File metadata and controls
28 lines (24 loc) · 708 Bytes
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
//! @Alan @pezy
//!
//! Exercise 11.20:
//! Rewrite the word-counting program from § 11.1 (p. 421) to use insert instead
//! of subscripting. Which program do you think is easier to write and read?
//! Explain your reasoning.
//!
#include <iostream>
#include <map>
#include <string>
using std::string;
int main()
{
std::map<string, size_t> word_count;
string word;
while (std::cin >> word) {
auto ret = word_count.insert({word, 1});
if (!ret.second) ++ret.first->second;
}
//! print the content of the map.
for (const auto& w : word_count)
std::cout << w.first << " " << w.second
<< ((w.second > 1) ? " times" : " time") << std::endl;
}