Problem overview
A shop keeps its stock in inventory, an unordered_map<string, int> from item names to counts. The function restock receives the map, an item and an amount. It adds amount to the count of item and returns the updated map. Every other entry stays as it is, and the order of the entries doesn't matter because an unordered_map has no useful order anyway.
For {"apples": 5, "bananas": 2, "cherries": 10}, item = "bananas" and amount = 3, the result is {"apples": 5, "bananas": 5, "cherries": 10}. A count of 0 is fine too: {"milk": 0} with 12 more milk becomes {"milk": 12}.
Approach
The expression inventory[item] doesn't only read a value. It gives access to the value stored under the key, so it can sit on the left side of an assignment like a variable. That makes inventory[item] += amount a complete update: the old count is read, amount is added, and the sum is stored back under the same key. No other entry is touched.
Compare it with a plain assignment: inventory[item] = amount would replace the count, and starting from {"pens": 7, "paper": 3} it would leave the pens at 1 instead of 8. Only += keeps the old count in the sum.
Step by step
- Add the amount. Use
+=oninventory[item]. The item is always already a key, so this changes an existing count. - Return the map. The map was changed in place, so returning it hands back every entry, with only the count of
itemdifferent.
The solution in C++
class Solution {
public:
unordered_map<string, int> restock(unordered_map<string, int>& inventory, string item, int amount) {
inventory[item] += amount;
return inventory;
}
};
What to know about this code:
- Missing keys start at zero. For a key that isn't in the map,
operator[]inserts it with a default value, and for anintthat value is 0. Soinventory["kiwis"] += 4on an empty map creates the entry"kiwis"with the count 4, and the map then has one entry. This lesson's item is always present, but the same line also works as "add to the count, starting from zero" in other programs. - The map is modified in place. The parameter is a reference, so the caller's map changes too. The function returns a copy of the same map.
- Arithmetic on the count is plain
intarithmetic. A signed overflow is undefined behaviour in C++, but the lesson's counts are small.
Edge cases and common mistakes
- Replacing instead of adding.
inventory[item] = amountwould store only the delivery and lose the old count. With{"pens": 7, "paper": 3},"pens"and 1, the right answer is 8, not 1. - Writing the item in quotes.
inventory["item"]targets a key spelled i, t, e, m and would add a new entry with the count of the amount. - A count of zero. A sold-out item still has an entry with the value 0, so
+=works on it like on any other count. - Looking at the order. An
unordered_mapdoesn't promise an order, so don't depend on one when you print it.
Testing
{"apples": 5, "bananas": 2, "cherries": 10},"bananas", 3 gives apples 5, bananas 5, cherries 10.{"milk": 0},"milk", 12 gives milk 12.{"pens": 7, "paper": 3},"pens", 1 gives pens 8, paper 3.
