* Task: Create a std::map with initial values :cpp:map:retrieval:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734300 :END: ** Front Write C++ code to create a ~std::map~ initialized with ~{{"apple", 1}, {"banana", 2}}~ ** Back #+begin_src c++ std::map m = {{"apple", 1}, {"banana", 2}}; #+end_src * Task: Access a map element safely with at() :cpp:map:retrieval:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734296 :END: ** Front Write code to safely access ~m.at("apple")~ from a ~std::map~ ** Back #+begin_src c++ std::map m = {{"apple", 1}}; int val = m.at("apple"); // returns 1, throws if not found #+end_src * Task: Insert into map with insert() :cpp:map:retrieval:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734292 :END: ** Front Write code to insert ~{"cherry", 3}~ into a ~std::map~ using ~insert()~ ** Back #+begin_src c++ auto [it, success] = m.insert({"cherry", 3}); // Returns pair — success is true if inserted #+end_src * Task: Insert or assign with insert_or_assign :cpp:map:retrieval:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734288 :END: ** Front Write code to insert ~{"apple", 99}~ or update it if it already exists ** Back #+begin_src c++ auto [it, inserted] = m.insert_or_assign("apple", 99); // If "apple" exists, updates value to 99 // inserted is false if key existed, true if newly inserted #+end_src * Task: Construct element in-place with emplace :cpp:map:retrieval:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734284 :END: ** Front Write code to construct a ~std::map>~ element in-place without copying ** Back #+begin_src c++ std::map> m; m.emplace(std::piecewise_construct, std::forward_as_tuple("nums"), std::forward_as_tuple(3, 1)); // vector with {1,1,1} #+end_src * Task: Insert only if key missing with try_emplace :cpp:map:retrieval:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734280 :END: ** Front Write code to only insert "banana" if it's NOT already in the map ** Back #+begin_src c++ m.try_emplace("banana", 99); // Does nothing if "banana" exists, inserts if missing // Unlike insert_or_assign, never overwrites existing value #+end_src * Task: Iterate over all key-value pairs :cpp:map:retrieval:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734276 :END: ** Front Write code to iterate over a ~std::map~ and print all key-value pairs ** Back #+begin_src c++ for (const auto& [key, val] : m) { std::cout << key << ": " << val << "\n"; } #+end_src * Task: Check if key exists with contains (C++20) :cpp:map:retrieval:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734272 :END: ** Front Write code to check if "apple" exists in a ~std::map~ using C++20 ** Back #+begin_src c++ if (m.contains("apple")) { // key exists } #+end_src * Task: Find element and check existence :cpp:map:retrieval:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734268 :END: ** Front Write code to find "apple" in a map and only process if found ** Back #+begin_src c++ auto it = m.find("apple"); if (it != m.end()) { // it->first = key, it->second = value int val = it->second; } #+end_src * Task: Erase element by key :cpp:map:retrieval:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734264 :END: ** Front Write code to remove the element with key "apple" from a ~std::map~ ** Back #+begin_src c++ size_t erased = m.erase("apple"); // Returns number of elements erased (0 or 1) #+end_src * Task: Range query with lower_bound and upper_bound :cpp:map:retrieval:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734260 :END: ** Front Write code to find all elements with keys between "apple" and "cherry" (inclusive) ** Back #+begin_src c++ auto [start, end] = m.equal_range("apple"); // lower_bound = first >= "apple" // upper_bound = first > "apple" for (auto it = start; it != end; ++it) { // process it->first, it->second } #+end_src * std::map vs std::unordered_map :cpp:map:retrieval:comparison: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734256 :END: ** Front What are the key differences between ~std::map~ and ~std::unordered_map~? ** Back - ~std::map~: sorted by key, O(log n) operations, uses red-black tree - ~std::unordered_map~: unsorted, O(1) avg operations, uses hash table - Use ~std::map~ when you need sorted iteration or range queries - Use ~std::unordered_map~ when you need fastest lookups and order doesn't matter * What is value_type for std::map? :cpp:map:retrieval:recognition: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734252 :END: ** Front What is the ~value_type~ of ~std::map~? ** Back ~std::pair~ * insert() return type :cpp:map:retrieval:recognition: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734247 :END: ** Front What does ~insert()~ return on a ~std::map~? ** Back ~std::pair~ - ~iterator~ points to the inserted (or existing) element - ~bool~ is ~true~ if insertion happened, ~false~ if key already existed * Why avoid operator[] for lookup? :cpp:map:retrieval:recognition: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734242 :END: ** Front Why is it risky to use ~map["missing"]~ to check if a key exists? ** Back ~operator[]~ inserts a default-constructed value if the key doesn't exist, modifying the map as a side effect. Use ~find()~ or ~contains()~ (C++20) for read-only lookup. * What does extract() do? (C++17) :cpp:map:retrieval:recognition: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734238 :END: ** Front What does ~m.extract(key)~ do on a ~std::map~? ** Back Removes the element with that key and returns a ~node_type~ (a node handle). The node can be inserted into another container without copying. O(log n) but no allocator overhead. * What does merge() do? (C++17) :cpp:map:retrieval:recognition: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1777821734232 :END: ** Front What does ~m1.merge(m2)~ do? ** Back Transfers elements from ~m2~ to ~m1~. For each element transferred, if a key already exists in ~m1~, that element stays in ~m2~. No element copies are made.