-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (46 loc) · 1.6 KB
/
main.cpp
File metadata and controls
61 lines (46 loc) · 1.6 KB
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
61
#include <string.h>
#include <iostream>
#include <map>
#include <utility>
#include <stdlib.h>
using namespace std;
int main()
{
// Compare (<) function not required since it is built into string class.
// else declaration would comparison function in multimap definition.
// i.e. multimap<string, int, compare> m;
multimap<string, int> m;
m.insert(pair<string, int>("a", 1));
m.insert(pair<string, int>("c", 2));
m.insert(pair<string, int>("b", 3));
m.insert(pair<string, int>("b", 4));
m.insert(pair<string, int>("a", 5));
m.insert(pair<string, int>("b", 6));
cout << "Number of elements with key a: " << m.count("a") << endl;
cout << "Number of elements with key b: " << m.count("b") << endl;
cout << "Number of elements with key c: " << m.count("c") << endl;
cout << "Elements in m: " << endl;
for (multimap<string, int>::iterator it = m.begin();
it != m.end();
++it)
{
cout << " [" << (*it).first << ", " << (*it).second << "]" << endl;
}
pair<multimap<string, int>::iterator, multimap<string, int>::iterator> ppp;
// equal_range(b) returns pair<iterator,iterator> representing the range
// of element with key b
ppp = m.equal_range("b");
// Loop through range of maps of key "b"
cout << endl << "Range of \"b\" elements:" << endl;
for (multimap<string, int>::iterator it2 = ppp.first;
it2 != ppp.second;
++it2)
{
cout << " [" << (*it2).first << ", " << (*it2).second << "]" << endl;
}
// Can't do this (??)
// cout << ppp.first << endl;
// cout << ppp.second << endl;
m.clear();
system("pause");
}