-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.cpp
130 lines (107 loc) · 4.87 KB
/
map.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <map>
#include <unordered_map>
#include <vector>
#include <string>
#include <chrono>
// Define the Contact structure
struct Contact {
std::string name;
std::string phoneNumber;
std::string email;
std::string city;
// Comparison operator for sorting in std::map
bool operator<(const Contact& other) const {
return name < other.name; // Sort alphabetically by name
}
};
// Custom hash function for Contact
struct ContactHash {
std::size_t operator()(const Contact& contact) const {
return std::hash<std::string>()(contact.name) ^
std::hash<std::string>()(contact.phoneNumber);
}
};
// Custom equality function for Contact
struct ContactEqual {
bool operator()(const Contact& lhs, const Contact& rhs) const {
return lhs.name == rhs.name && lhs.phoneNumber == rhs.phoneNumber;
}
};
int main() {
// ✅ Basic std::map operations
std::map<std::string, Contact> contactList;
contactList["Alice"] = {"Alice", "123-456-7890", "[email protected]", "New York"};
contactList["Bob"] = {"Bob", "987-654-3210", "[email protected]", "Los Angeles"};
contactList["Charlie"] = {"Charlie", "555-555-5555", "[email protected]", "Chicago"};
contactList["David"] = {"David", "111-222-3333", "[email protected]", "New York"};
std::cout << "Basic map operations:\n";
for (const auto& [name, contact] : contactList) {
std::cout << name << ": " << contact.phoneNumber << ", " << contact.city << "\n";
}
// ✅ Using lower_bound and upper_bound
std::cout << "\nUsing lower_bound and upper_bound:\n";
auto lower = contactList.lower_bound("Bob");
auto upper = contactList.upper_bound("Charlie");
std::cout << "Contacts from 'Bob' to 'Charlie':\n";
for (auto it = lower; it != upper; ++it) {
std::cout << it->first << ": " << it->second.phoneNumber << "\n";
}
// ✅ Range-based erase in map
contactList.erase(contactList.find("Bob"), contactList.find("David"));
std::cout << "\nAfter range-based erase (Bob to David):\n";
for (const auto& [name, contact] : contactList) {
std::cout << name << ": " << contact.phoneNumber << "\n";
}
// ✅ Using custom struct as key in unordered_map with custom hash function
std::unordered_map<Contact, std::string, ContactHash, ContactEqual> contactCities;
contactCities[{"Alice", "123-456-7890", "[email protected]", "New York"}] = "New York";
contactCities[{"Bob", "987-654-3210", "[email protected]", "Los Angeles"}] = "Los Angeles";
std::cout << "\nCustom struct as key in unordered_map:\n";
for (const auto& [contact, city] : contactCities) {
std::cout << contact.name << ": " << city << "\n";
}
// ✅ Bucket information in unordered_map
std::cout << "\nBucket information:\n";
std::cout << "Bucket count: " << contactCities.bucket_count() << "\n";
std::cout << "Bucket of Alice: " <<
contactCities.bucket({"Alice", "123-456-7890", "[email protected]", "New York"}) << "\n";
// ✅ Comparing iteration order of map vs unordered_map
std::map<std::string, std::string> orderedCities;
std::unordered_map<std::string, std::string> unorderedCities;
orderedCities["Charlie"] = "Chicago";
orderedCities["Alice"] = "New York";
orderedCities["Bob"] = "Los Angeles";
unorderedCities["Charlie"] = "Chicago";
unorderedCities["Alice"] = "New York";
unorderedCities["Bob"] = "Los Angeles";
std::cout << "\nOrdered map iteration:\n";
for (const auto& [name, city] : orderedCities) {
std::cout << name << ": " << city << "\n";
}
std::cout << "\nUnordered map iteration:\n";
for (const auto& [name, city] : unorderedCities) {
std::cout << name << ": " << city << "\n";
}
// ✅ Comparing performance of map vs unordered_map
const int N = 100000;
std::map<std::string, Contact> perfMap;
std::unordered_map<std::string, Contact> perfUnorderedMap;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; ++i) {
perfMap[std::to_string(i)] = {"Contact" + std::to_string(i), "123-456-7890", "[email protected]", "City"};
}
auto end = std::chrono::high_resolution_clock::now();
std::cout << "\nTime to insert " << N << " elements in map: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
<< " ms\n";
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; ++i) {
perfUnorderedMap[std::to_string(i)] = {"Contact" + std::to_string(i), "123-456-7890", "[email protected]", "City"};
}
end = std::chrono::high_resolution_clock::now();
std::cout << "Time to insert " << N << " elements in unordered_map: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
<< " ms\n";
return 0;
}