|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +void set_example(){ |
| 5 | + set<int> s; |
| 6 | + s.insert(-10); s.insert(100); s.insert(15); // {-10, 15, 100} |
| 7 | + s.insert(-10); // {-10, 15, 100} |
| 8 | + cout << s.erase(100) << '\n'; // {-10, 15}, 1 |
| 9 | + cout << s.erase(20) << '\n'; // {-10, 15}, 0 |
| 10 | + if(s.find(15) != s.end()) cout << "15 in s\n"; |
| 11 | + else cout << "15 not in s\n"; |
| 12 | + cout << s.size() << '\n'; // 2 |
| 13 | + cout << s.count(50) << '\n'; // 0 |
| 14 | + for(auto e : s) cout << e << ' '; |
| 15 | + cout << '\n'; |
| 16 | + s.insert(-40); // {-40, -10, 15} |
| 17 | + set<int>::iterator it1 = s.begin(); // {-40(<-it1), -10, 15} |
| 18 | + it1++; // {-40, -10(<-it1), 15} |
| 19 | + auto it2 = prev(it1); // {-40(<-it2), -10, 15} |
| 20 | + it2 = next(it1); // {-40, -10, 15(<-it2)} |
| 21 | + advance(it2, -2); // {-40(<-it2), -10, 15} |
| 22 | + auto it3 = s.lower_bound(-20); // {-40, -10(<-it3), 15} |
| 23 | + auto it4 = s.find(15); // {-40, -10, 15(<-it4)} |
| 24 | + cout << *it1 << '\n'; // -10 |
| 25 | + cout << *it2 << '\n'; // -40 |
| 26 | + cout << *it3 << '\n'; // -10 |
| 27 | + cout << *it4 << '\n'; // 15 |
| 28 | +} |
| 29 | + |
| 30 | +void multiset_example(){ |
| 31 | + multiset<int> ms; |
| 32 | + // {-10, 15, 100} |
| 33 | + ms.insert(-10); ms.insert(100); ms.insert(15); // {-10, -10, 15, 15, 100} |
| 34 | + ms.insert(-10); ms.insert(15); |
| 35 | + cout << ms.size() << '\n'; // 5 |
| 36 | + for(auto e : ms) cout << e << ' '; |
| 37 | + cout << '\n'; |
| 38 | + cout << ms.erase(15) << '\n'; // {-10, -10, 100}, 2 |
| 39 | + ms.erase(ms.find(-10)); // {-10, 100} |
| 40 | + ms.insert(100); // {-10, 100, 100} |
| 41 | + cout << ms.count(100) << '\n'; // 2 |
| 42 | + auto it1 = ms.begin(); // {-10(<-it1), 100, 100} |
| 43 | + auto it2 = ms.upper_bound(100); // {-10, 100, 100} (<-it2) |
| 44 | + auto it3 = ms.find(100); // {-10, 100(<-it3), 100} |
| 45 | + cout << *it1 << '\n'; // -10 |
| 46 | + cout << (it2 == ms.end()) << '\n'; // 1 |
| 47 | + cout << *it3 << '\n'; // 100 |
| 48 | +} |
| 49 | + |
| 50 | +void map_example(){ |
| 51 | + map<string, int> m; |
| 52 | + m["hi"] = 123; |
| 53 | + m["bkd"] = 1000; |
| 54 | + m["gogo"] = 165; // ("bkd", 1000), ("gogo", 165), ("hi", 123) |
| 55 | + cout << m.size() << '\n'; // 3 |
| 56 | + m["hi"] = -7; // ("bkd", 1000), ("gogo", 165), ("hi", -7) |
| 57 | + if(m.find("hi") != m.end()) cout << "hi in m\n"; |
| 58 | + else cout << "hi not in m\n"; |
| 59 | + m.erase("bkd"); // ("gogo", 165), ("hi", 123) |
| 60 | + for(auto e : m) |
| 61 | + cout << e.first << ' ' << e.second << '\n'; |
| 62 | + auto it1 = m.find("gogo"); |
| 63 | + cout << it1->first << ' ' << it1->second << '\n'; // gogo 165 |
| 64 | +} |
| 65 | + |
| 66 | +int main(){ |
| 67 | + set_example(); |
| 68 | + multiset_example(); |
| 69 | + map_example(); |
| 70 | +} |
0 commit comments