-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp2_2.html
More file actions
84 lines (72 loc) · 3.21 KB
/
Copy pathCpp2_2.html
File metadata and controls
84 lines (72 loc) · 3.21 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<article>
<h1>C++ II: Page 2 - STL: Maps & Sets</h1>
<section>
<h2>Associative Containers</h2>
<p>Associative containers in the STL, such as <code>std::map</code> and <code>std::set</code>, are designed for fast lookup, insertion, and deletion of elements. Maps store key-value pairs, while sets store unique keys.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: associative.cpp
* Demonstration of std::map, std::unordered_map, std::set, and std::multimap
*/
#include <iostream>
#include <map>
#include <unordered_map>
#include <set>
#include <string>
using namespace std;
int main() {
// 1. std::map (Ordered key-value, balanced tree)
map<string, int> phoneBook;
phoneBook["Alice"] = 12345;
phoneBook["Bob"] = 67890;
// 2. std::unordered_map (Unordered key-value, hash table)
unordered_map<string, string> capitalCities;
capitalCities["France"] = "Paris";
capitalCities["Japan"] = "Tokyo";
// 3. std::set (Unique, ordered keys)
set<int> uniqueIds = {1, 2, 3, 1, 2}; // 1 is stored once
// 4. std::multimap (Multiple values for one key)
multimap<string, string> userRoles;
userRoles.insert({"Alice", "Admin"});
userRoles.insert({"Alice", "Editor"});
// 5. Lookup
if (phoneBook.find("Alice") != phoneBook.end()) {
cout << "Alice's number: " << phoneBook["Alice"] << endl;
}
// 6. Iteration (Structured binding C++17)
cout << "Phonebook entries: " << endl;
for (const auto& [name, number] : phoneBook) {
cout << name << ": " << number << endl;
}
// 7. Erasing elements
uniqueIds.erase(2);
return 0;
}
/*
* Associative Container Nuances:
* 1. map: O(log n) access time, keys are sorted.
* 2. unordered_map: O(1) average access time (hash table).
* 3. set: O(log n) insertion/deletion, guarantees unique, sorted elements.
* 4. multimap: Allows duplicate keys.
* 5. Structured binding (C++17) simplifies map iteration (const auto& [key, val]).
* 6. Prefer unordered_map unless ordering is explicitly required.
* 7. Erasing elements from a map invalidates only iterators to the erased element.
*/
</code></pre>
<div class="interactive-sim">
<h3>Map Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp2_2');
out.innerHTML = 'map["Alice"] = 123;<br>';
setTimeout(() => out.innerHTML += '>> Alice number is ' + 123, 800);
">Run Simulation</button>
<div id="sim-output-cpp2_2" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp2_1.html" style="text-decoration: none; color: #6c757d;">← Previous: STL: Vectors</a>
<a href="#" data-file="Cpp2_3.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: STL: Iterators →</a>
</div>
</footer>
</article>