A stock ledger passed by reference
Keep a warehouse stock as item name → count in a std::map and process n commands in order.
Write one function per command; functions that change the stock take the map by reference (&), and functions that only read it take a const reference.
- add name k: add k of that item and print the new count.
- get name: print the count of that item.
For an unknown item print 0 and do not add it to the ledger.
- use name k: if there are at least k, remove k and print the count left.
If the count left is 0, delete the item from the ledger.
If there are fewer than k or the item is unknown, change nothing and print -1.
After all commands, print the number m of items left in the ledger, then m lines "name count" in dictionary order of the names (plain comparison of lowercase strings).
- Input
- First line n (1 ≤ n ≤ 1000), then one command per line for n lines.
Names are 1 to 10 lowercase letters, 1 ≤ k ≤ 1000 - Output
- One line per command, then m, then m lines of "name count"
7 add apple 5 add kiwi 2 get pear use apple 3 use kiwi 5 use kiwi 2 get apple
5 2 0 2 -1 0 2 1 apple 2