-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
61 lines (49 loc) · 1.85 KB
/
Map.cpp
File metadata and controls
61 lines (49 loc) · 1.85 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 <bits/stdc++.h>
using namespace std;
// map is an associative container that contains a sorted set of unique keys & their values.
void solve() {
// --- Initialization ---
map<int, char> mp1 = {{1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}};
map<int, char, greater<int>> mp2 = {{4, 'd'}, {3, 'c'}, {2, 'b'}, {1, 'a'}};
// --- Iterators ---
// begin(), end()
// rbegin(), rend()
// --- Capacity ---
// size(), empty()
// --- Element accessa ---
// [key], at(key)
// --- Modifiers ---
// insert({key, value}), insert(initializer_list), insert(it_first, it_last),
// insert(pos, {key, value}), emplace(args...), emplace_hint(pos, args...)
// insert_or_assign({key, value})
// erase(key), erase(pos), erase(pos_first, pos_last)
// merge(map) -> O(n*log(n))
// clear()
// --- Lookup ---
// count(key), contains(key)
// find(key)
// lower_bound(key) -> The first element greater than or equal to key.
// upper_bound(key) -> The first element greater than to key.
map<int, char>::iterator lb = mp1.lower_bound(2);
map<int, char>::iterator ub = mp1.upper_bound(2);
cout << lb->first << ' ' << lb->second << '\n';
cout << ub->first << ' ' << ub->second << '\n';
// --- Hints ---
// insert(pos, {key, value}), emplace_hint(pos, args...), erase(pos)
// O(1) if the insertion happens in the position just after or before pos, O(log(n)) otherwise.
// When using a custom class with a map, it's necessary to define a comparator for that class,
// as the default comparison often relies on the less-than operator (<).
for (auto [key, value] : mp1) {
cout << key << ' ' << value << '\n';;
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}