forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex11_20.cpp
More file actions
29 lines (26 loc) · 696 Bytes
/
Copy pathex11_20.cpp
File metadata and controls
29 lines (26 loc) · 696 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
29
// @Yue Wang @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;
using std::map;
using std::cin;
using std::cout;
int main()
{
map<string, size_t> counts;
for(string word; cin >> word;)
{
auto result = counts.insert({ word, 1 });
if(!result.second)
++result.first->second;
}
for(auto const& count : counts)
cout << count.first << " " << count.second << ((count.second > 1) ? " times\n" : " time\n");
}