add set
This commit is contained in:
+383
@@ -0,0 +1,383 @@
|
|||||||
|
* std::set empty initialization :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835132
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to create an empty std::set<int>
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
std::set<int> s;
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set initializer list :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835127
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to create a std::set from initializer list {3, 1, 2}
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
std::set<int> s = {3, 1, 2}; // contents: {1, 2, 3} (sorted!)
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set copy constructor :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835123
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to copy construct a std::set from existing set s2
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
std::set<int> s1(s2); // or: s1 = s2;
|
||||||
|
std::set<int> s1 = s2;
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set from range :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835120
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to construct std::set from iterator range of another set
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
std::set<int> s2(++s1.begin(), s1.end());
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set C++17 deduction :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835116
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++17 deduction guide to create set from another set
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
std::set s3 = s2; // deduces std::set<int>
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set insert return type :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835112
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to insert 5 into std::set and check if it succeeded
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
auto [it, success] = s.insert(5);
|
||||||
|
if (!success) {
|
||||||
|
// already exists
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set insert with hint :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835107
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to insert 5 near begin() as a hint
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
s.insert(s.begin(), 5); // returns iterator only (C++11+)
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set emplace vs insert :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835103
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to emplace {5, 6} directly into a std::set without copying
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
s.emplace(5); // constructs in-place
|
||||||
|
s.emplace(std::piecewise_construct,
|
||||||
|
std::forward_as_tuple(5),
|
||||||
|
std::forward_as_tuple());
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set erase by key :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835099
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to erase key 5 from std::set and check count removed
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
size_t n = s.erase(5); // returns count erased (0 or 1)
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set erase by iterator :cpp:set:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835095
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to erase element at iterator it
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
it = s.erase(it); // returns iterator to next element
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set erase range :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835091
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to erase all elements from begin() to end()
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
s.erase(s.begin(), s.end()); // clears entire set
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set find :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835088
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to check if key 5 exists in set using find()
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
if (s.find(5) != s.end()) {
|
||||||
|
// found
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set count :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835084
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to check if key 5 exists using count() (set returns 0 or 1)
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
if (s.count(5) == 1) {
|
||||||
|
// exists
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set contains (C++20) :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835079
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++20 to check if key 5 exists using contains()
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
if (s.contains(5)) {
|
||||||
|
// found
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set extract (C++17) :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835075
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to extract key 5 from set and reinsert it
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
auto node = s.extract(5); // removes and returns node_handle
|
||||||
|
s.insert(std::move(node));
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set merge (C++17) :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835072
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to merge elements from set s2 into s1
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
s1.merge(s2); // transfers matching elements from s2 to s1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set begin/end iterators :cpp:set:iterators:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835067
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to iterate over all elements in std::set
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
for (const auto& elem : s) {
|
||||||
|
std::cout << elem << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
// or with iterators:
|
||||||
|
for (auto it = s.begin(); it != s.end(); ++it) {
|
||||||
|
std::cout << *it << '\n';
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set reverse iterators :cpp:set:iterators:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835062
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to iterate backwards through std::set
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
for (auto it = s.rbegin(); it != s.rend(); ++it) {
|
||||||
|
std::cout << *it << '\n'; // prints in descending order
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set const_iterator :cpp:set:iterators:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835057
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to get const_iterator to first element
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
std::set<int>::const_iterator it = s.cbegin(); // C++11+
|
||||||
|
auto it = s.cbegin(); // auto also works
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set lower_bound :cpp:set:lookup:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835053
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to find first element >= 5 in std::set
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
auto it = s.lower_bound(5);
|
||||||
|
if (it != s.end() && *it == 5) {
|
||||||
|
// found exact match
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set upper_bound :cpp:set:lookup:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835049
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to find first element > 5 in std::set
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
auto it = s.upper_bound(5); // skips past all elements == 5
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set equal_range :cpp:set:lookup:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835045
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to get pair of iterators for elements matching 5
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
auto [lo, hi] = s.equal_range(5);
|
||||||
|
// lo = lower_bound(5), hi = upper_bound(5)
|
||||||
|
if (lo != hi) {
|
||||||
|
// found element(s)
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set insert multiple from initializer list :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835042
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to insert multiple elements {1, 2, 3} at once
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
s.insert({1, 2, 3});
|
||||||
|
// or with pair return (C++17):
|
||||||
|
auto [it, success] = s.insert({1, 2, 3});
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set key_comp and value_comp :cpp:set:observers:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835037
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to get the comparison object from std::set
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
std::set<int>::key_compare comp = s.key_comp();
|
||||||
|
std::set<int>::value_compare vcomp = s.value_comp(); // same for set
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set empty and size :cpp:set:capacity:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835032
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to check if set is empty and get size
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
bool b = s.empty();
|
||||||
|
size_t n = s.size();
|
||||||
|
size_t max = s.max_size();
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set clear :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835028
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to remove all elements from std::set
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
s.clear(); // equivalent to s.erase(s.begin(), s.end())
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set swap :cpp:set:retrieval:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835025
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ to swap contents of two sets
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
s1.swap(s2); // or: s1.swap(s2);
|
||||||
|
std::swap(s1, s2);
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* std::set O(log n) operations summary :cpp:set:complexity:retrieval:understanding:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835020
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
What is the time complexity of insert, find, erase in std::set?
|
||||||
|
** Back
|
||||||
|
O(log n) for all operations. std::set is implemented as a red-black tree.
|
||||||
|
|
||||||
|
* std::set properties :cpp:set:retrieval:understanding:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776613835015
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
What are the key properties of std::set?
|
||||||
|
** Back
|
||||||
|
- No duplicate keys (use std::multiset for duplicates)
|
||||||
|
- Keys are sorted (ascending by default)
|
||||||
|
- Implemented as red-black tree
|
||||||
|
- Elements cannot be modified through iterators (const)
|
||||||
Reference in New Issue
Block a user