forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex11_18.cpp
32 lines (26 loc) · 869 Bytes
/
ex11_18.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
31
32
//! @Alan
//!
//! Exercise 11.18:
//! Write the type of map_it from the loop on page 430 without using auto or
//! decltype.
//!
#include <iostream>
#include <map>
#include <string>
int main()
{
std::map<std::string, size_t> word_count;
//! the orignal codes:
// auto map_it = word_count.cbegin();
std::map<std::string, size_t>::const_iterator map_it = word_count.cbegin();
//! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//! the type ex11.18 required.
// compare the current iterator to the off-the-end iterator
while (map_it != word_count.cend()) {
// dereference the iterator to print the element key--value pairs
std::cout << map_it->first << " occurs " << map_it->second << " times"
<< std::endl;
++map_it; // increment the iterator to denote the next element
}
return 0;
}