forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex11_27_28_29_30.cpp
60 lines (55 loc) · 1.83 KB
/
ex11_27_28_29_30.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! @Alan
//!
//! Exercise 11.27:
//! What kinds of problems would you use count to solve?
//! When might you use find instead?
// I would use count to deal with multimap or multi multiset.
// As for the associative container that have unique key, I would use find
// instead of count.
//!
//! Exercise 11.28:
//! Define and initialize a variable to hold the result of
//! calling find on a map from string to vector of int.
//!
//! Exercise 11.29:
//! What do upper_bound, lower_bound, and equal_range return
//! when you pass them a key that is not in the container?
// If the element is not in the multimap, then lower_bound
// and upper_bound will return equal iterators; both will
// refer to the point at which the key can be inserted without
// disrupting the order.
//
// If no matching element is found, then both the first and
// second iterators refer to the position where this key can
// be inserted.
//!
//! Exercise 11.30:
//! Explain the meaning of the operand pos.first->second used
//! in the output expression of the final program in this section.
//! cout << pos.first->second << endl;
// ^^^^^^^^^^^^^^^^^
// pos a pair
// pos.first the iterator refering to the first element with the
// matching key
// pos.first->second the value part of the key-value of the first element
// with the matching key
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
int main()
{
std::map<std::string, std::vector<int>> m;
m = {{"Alan",
{
1, 2, 3, 4, 5,
}},
{"John", {1, 5, 6, 7, 8}}};
//! ex11.28
std::map<std::string, std::vector<int>>::iterator it;
//! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//! type used to define this iterator.
it = m.find("Alan");
return 0;
}