-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
62 lines (47 loc) · 1.36 KB
/
Copy pathmain.cpp
File metadata and controls
62 lines (47 loc) · 1.36 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
62
#include "hash_map.h"
#include <iostream>
#include <map>
using namespace ProCpp;
using namespace std;
int main()
{
//hash_map<string, int> myHash;
//myHash.insert(make_pair("KeyOne", 100));
//myHash.insert(make_pair("KeyTwo", 200));
//myHash.insert(make_pair("KeyThree", 300));
hash_map<string, int> myHash{
{ "KeyOne", 100 },
{ "KeyTwo", 200 }
};
myHash.insert({
{ "KeyThree", 300 },
{ "KeyFour", 400 }
});
for (auto it = myHash.cbegin(); it != myHash.cend(); ++it) {
// Use both -> and * to test the operations
cout << it->first << " maps to " << (*it).second << endl;
}
cout << "----" << endl;
auto found = myHash.find("KeyThree");
if (found != end(myHash))
{
cout << "Found KeyThree: value = " << found->second << endl;
}
cout << "----" << endl;
// Print elements using range-based for loop
for (auto& p : myHash) {
cout << p.first << " maps to " << p.second << endl;
}
cout << "----" << endl;
// Create a map with all the elements in the hashmap
map<string, int> myMap(cbegin(myHash), cend(myHash));
for (auto& p : myMap) {
cout << p.first << " maps to " << p.second << endl;
}
hash_map<string, int> myHash2;
myHash.swap(myHash2);
hash_map<string, int> myHash3(std::move(myHash2));
cout << myHash3.size() << endl;
cout << myHash3.max_size() << endl;
return 0;
}