A little game's Bag keeps how many of each thing it holds in items. Write use(item): if there's none of it, return false; otherwise take one away and return true, and when it gets to 0, erase it from the bag.
bag_after({"potion", "potion", "key"}, {"potion"})→{'key': 1, 'potion': 1}bag_after({"key"}, {"key"})→{}map<string, int> bag_after(const vector<string> &picks, const vector<string> &uses)#include <map>
#include <string>
#include <vector>
using namespace std;
class Bag {
public:
map<string, int> items;
void pick(const string &item) {
items[item]++;
}
bool use(const string &item) {
// your code here: use up one item
return false;
}
};
map<string, int> bag_after(const vector<string> &picks, const vector<string> &uses) {
Bag b;
for (const string &p : picks) {
b.pick(p);
}
for (const string &u : uses) {
b.use(u);
}
return b.items;
}
Run tries the examples. When they look right, Submit.