6.3 KiB
- Task: Create a std::map with initial values
- Task: Access a map element safely with at()
- Task: Insert into map with insert()
- Task: Insert or assign with insert_or_assign
- Task: Construct element in-place with emplace
- Task: Insert only if key missing with try_emplace
- Task: Iterate over all key-value pairs
- Task: Check if key exists with contains (C++20)
- Task: Find element and check existence
- Task: Erase element by key
- Task: Range query with lower_bound and upper_bound
- std::map vs std::unordered_map
- What is value_type for std::map?
- insert() return type
- Why avoid operator[] for lookup?
- What does extract() do? (C++17)
- What does merge() do? (C++17)
Task: Create a std::map with initial values cpp map retrieval production
Front
Write C++ code to create a std::map<std::string, int> initialized with {{"apple", 1}, {"banana", 2}}
Back
std::map<std::string, int> m = {{"apple", 1}, {"banana", 2}};
Task: Access a map element safely with at() cpp map retrieval production
Front
Write code to safely access m.at("apple") from a std::map<std::string, int>
Back
std::map<std::string, int> m = {{"apple", 1}};
int val = m.at("apple"); // returns 1, throws if not found
Task: Insert into map with insert() cpp map retrieval production
Front
Write code to insert {"cherry", 3} into a std::map<std::string, int> using insert()
Back
auto [it, success] = m.insert({"cherry", 3});
// Returns pair<iterator, bool> — success is true if inserted
Task: Insert or assign with insert_or_assign cpp map retrieval production
Front
Write code to insert {"apple", 99} or update it if it already exists
Back
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
Task: Construct element in-place with emplace cpp map retrieval production
Front
Write code to construct a std::map<std::string, std::vector<int>> element in-place without copying
Back
std::map<std::string, std::vector<int>> m;
m.emplace(std::piecewise_construct,
std::forward_as_tuple("nums"),
std::forward_as_tuple(3, 1)); // vector with {1,1,1}
Task: Insert only if key missing with try_emplace cpp map retrieval production
Front
Write code to only insert "banana" if it's NOT already in the map
Back
m.try_emplace("banana", 99);
// Does nothing if "banana" exists, inserts if missing
// Unlike insert_or_assign, never overwrites existing value
Task: Iterate over all key-value pairs cpp map retrieval production
Front
Write code to iterate over a std::map<std::string, int> and print all key-value pairs
Back
for (const auto& [key, val] : m) {
std::cout << key << ": " << val << "\n";
}
Task: Check if key exists with contains (C++20) cpp map retrieval production
Front
Write code to check if "apple" exists in a std::map<std::string, int> using C++20
Back
if (m.contains("apple")) {
// key exists
}
Task: Find element and check existence cpp map retrieval production
Front
Write code to find "apple" in a map and only process if found
Back
auto it = m.find("apple");
if (it != m.end()) {
// it->first = key, it->second = value
int val = it->second;
}
Task: Erase element by key cpp map retrieval production
Front
Write code to remove the element with key "apple" from a std::map
Back
size_t erased = m.erase("apple");
// Returns number of elements erased (0 or 1)
Task: Range query with lower_bound and upper_bound cpp map retrieval production
Front
Write code to find all elements with keys between "apple" and "cherry" (inclusive)
Back
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
}
std::map vs std::unordered_map cpp map retrieval comparison
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 treestd::unordered_map: unsorted, O(1) avg operations, uses hash table- Use
std::mapwhen you need sorted iteration or range queries - Use
std::unordered_mapwhen you need fastest lookups and order doesn't matter
What is value_type for std::map? cpp map retrieval recognition
Front
What is the value_type of std::map<Key, T>?
Back
std::pair<const Key, T>
insert() return type cpp map retrieval recognition
Front
What does insert() return on a std::map?
Back
std::pair<iterator, bool>
iteratorpoints to the inserted (or existing) elementboolistrueif insertion happened,falseif key already existed
Why avoid operator[] for lookup? cpp map retrieval recognition
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
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
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.