小さなゲームの Bag は、持ち物ごとの個数を items に持っています。use(item) を書きましょう。1 つもなければ false を返し、あれば 1 つ減らして true を返します。0 になったらバッグから erase します。
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) {
// ここにコードを書く:アイテムを 1 つ使う
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;
}
実行すると例で試せます。よさそうなら提出しましょう。