The last lesson found a task by walking the whole vector and comparing IDs. That works, but it has two weaknesses. Every lookup visits every task, and nothing stops two tasks with the same ID from being stored, after which "find task 2" has two possible answers.
std::map<Key, Value> from <map> fixes both. It stores key/value pairs with unique keys, kept in sorted key order. Looking up a key takes a number of steps that grows with the logarithm of the size (about 10 comparisons for 1000 tasks) instead of the size itself. For the tracker, the key is the task ID and the value is the task, so identity becomes the way the data is organized rather than a field you search for.
The one thing to learn carefully is how a map behaves when a key is missing or already present, because the different member functions make different choices.
Predict and run
emplace tries to insert a pair. The second call uses a key that is already there. What is printed?
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> titles;
titles.emplace(7, "Read");
auto insertion = titles.emplace(7, "Replace");
auto found = titles.find(7);
std::cout << insertion.second << " " << found->second << "\n";
return 0;
}Check your prediction
0 Reademplace never replaces. It returns a pair: .first is an iterator to the entry with that key (old or new) and .second is true only if it inserted. The duplicate left "Read" in place and reported false, printed as 0. That is exactly the tracker's duplicate policy: the original task survives.
find returns a position, or end
find(key) returns an iterator, an object that acts like a position in the container. For a map, the iterator refers to a pair: ->first is the key and ->second is the value. When the key is absent, find returns end(), a position one past the last entry. end() refers to no element, so dereferencing it is undefined behavior:
auto found = titles.find(99);
std::cout << found->second << "\n"; // Undefined behavior when 99 is missing.Always compare with end() first. When you only need a yes or no, C++20's titles.contains(99) is clearer.
operator[] inserts missing keys
titles[id] looks convenient, but its job is "give me a reference to the value for this key, creating it if needed". Reading a missing key inserts a default-constructed value, here an empty string:
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> titles{{7, "Read"}};
std::cout << "Size before: " << titles.size() << "\n";
if (titles[99].empty()) { std::cout << "99 has no title\n"; }
std::cout << "Size after: " << titles.size() << "\n";
std::cout << "Contains 99: " << titles.contains(99) << "\n";
return 0;
}Size before: 1
99 has no title
Size after: 2
Contains 99: 1The program asked a question and changed the data. In the tracker, that would create a task with no title and zero minutes, breaking every invariant from Day 1. operator[] has two more limits: it does not compile on a const map, because it might insert, and it does not compile when the value type has no default constructor. The project's Task has only a private constructor, so tasks[id] would not even build. Use find to look up and emplace to insert on purpose.
Insert, replace, remove
The three ways to put a value in a map differ in what they do with an existing key:
emplace(key, value)inserts only if the key is new and reports which happened.insert_or_assign(key, value)inserts or overwrites, and reports which happened.map[key] = valueinserts or overwrites, and reports nothing.
erase(key) removes the entry and returns how many it removed (0 or 1), so a missing key is easy to detect. Removing one key never changes any other entry. Iterating a map visits entries in ascending key order, whatever order they were inserted in, and structured bindings name the two halves of each pair:
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> titles{{9, "Practice"}, {3, "Read"}, {8, "Review"}};
std::cout << "Erased 3: " << titles.erase(3) << "\n";
std::cout << "Erased 3 again: " << titles.erase(3) << "\n";
for (const auto& [id, title] : titles) { std::cout << id << " " << title << "\n"; }
return 0;
}Erased 3: 1
Erased 3 again: 0
8 Review
9 Practiceconst auto& binds to each pair without copying it. The keys inside a map are always const, because changing a key in place would break the sorted order; to "rename" a key you erase and insert.
Practice
Your turn
Write int load(std::map<int, std::string>& titles, const std::vector<std::pair<int, std::string>>& rows) that inserts each row and returns how many were rejected as duplicates. Load {8, "Review"}, {3, "Read"}, {8, "Replace"}, {5, "Practice"}, print the count and the map in key order, then erase 3 and report whether find(3) equals end():
Rejected: 1
3 Read
5 Practice
8 Review
After erase, 3 missing: yes#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
// Write load here.
int main() {
std::map<int, std::string> titles;
return 0;
}Hint 1: the idea
The map already enforces unique keys. Your function only has to ask it to insert without replacing and count the times it refused.
Hint 2: a first step
Loop over the rows with for (const auto& [id, title] : rows) and test if (!titles.emplace(id, title).second). Do not use titles[id] = title, which would silently replace "Review" with "Replace".
Show one solution
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
int load(std::map<int, std::string>& titles, const std::vector<std::pair<int, std::string>>& rows) {
int rejected = 0;
for (const auto& [id, title] : rows) {
if (!titles.emplace(id, title).second) { ++rejected; }
}
return rejected;
}
int main() {
std::map<int, std::string> titles;
const int rejected = load(titles, {{8, "Review"}, {3, "Read"}, {8, "Replace"}, {5, "Practice"}});
std::cout << "Rejected: " << rejected << "\n";
for (const auto& [id, title] : titles) { std::cout << id << " " << title << "\n"; }
titles.erase(3);
std::cout << "After erase, 3 missing: " << (titles.find(3) == titles.end() ? "yes" : "no") << "\n";
return 0;
}Rejected: 1
3 Read
5 Practice
8 Review
After erase, 3 missing: yesThe second 8 was refused, so "Review" survived; with operator[] the output would show "8 Replace" and zero rejections. The listing comes out in key order 3, 5, 8 even though the rows arrived as 8, 3, 8, 5, because a map keeps itself sorted. The next lesson replaces the bare string values with a named state, so a task can be todo or done without magic numbers.
Completion is saved in this browser only. It does not measure mastery.