One key, one record
Use maps without confusing lookup, insertion, and replacement.
Two columns with a unique key
std::map<Key, Value>, from <map>, stores one mapped value per unique key and traverses entries by key. Equal mapped scores do not merge records. A name-to-score map orders names, not scores. There is no standard std::ordered_map spelling.
Decide what a repeated key should mean. m[key] = value keeps the latest value; m[key] += amount accumulates; insert({key, value}) keeps the first. insert_or_assign explicitly replaces, while try_emplace preserves an existing mapped value. Argument expressions are still evaluated before the call.
A read-looking expression can insert
m[key] returns the mapped value by reference. If absent, the key is first inserted with a default value: zero for an integer, empty for a string or vector. This is useful for counting and grouping; it is often wrong for read-only queries.
find returns end() when absent. contains returns existence. at returns the value but throws std::out_of_range for an absent key. count is 0 or 1 for map entries; if m[7] stores 12, then count(7) is 1 and at(7) is 12. Never use a mapped zero as proof of absence.
// Complete C++23 program. Input: n q, n name-score pairs, q names.
#include <iostream>
#include <map>
#include <string>
int main() {
int n, q;
std::cin >> n >> q;
std::map<std::string, int> scores;
for (int i = 0; i < n; ++i) {
std::string name;
int score;
std::cin >> name >> score;
scores[name] = score;
}
while (q--) {
std::string name;
std::cin >> name;
auto it = scores.find(name);
if (it == scores.end()) std::cout << "MISSING\n";
else std::cout << it->second << '\n';
}
}Modify values, preserve keys
A map iterator exposes a key–value pair: first is a const key and second is the mapped value. for (auto& entry : scores) changes actual records; for (auto entry : scores) changes only copies. Use const auto& for read-only traversal to avoid copying key text or large mapped values.
A read-only helper should take const std::map<...>& to avoid copying the whole table. It cannot use operator[] through that const reference. Standard map iterator erasure follows the same guarded return-next pattern as set erasure.
Word frequency map
Split on whitespace. Keys are case-sensitive; punctuation stays attached. Displayed in sorted key order like std::map<string, int>.
code3learn2solve16 words · 3 distinct keys
Guided simulation · Change the inputs to test your understanding.
An empty map<string,int> m executes cout << m["nour"]. What has happened afterward?
Turn the idea into code
The updates for one product are 10, 4, 7. Give the result for keeping the first price, latest price, and total amount, and identify an operation for each.
Give me a hint
The declaration can stay the same; the repeated-key policy is expressed by the update.
Show the explained solution
First: 10 using insert or try_emplace. Latest: 7 using assignment or insert_or_assign. Total: 21 using +=. Query with find when absence is possible, so inspecting an unknown product does not add a record.
Your notes
Mark complete when you can explain it yourself.