Kodewick
学びはじめる →
ミニチャレンジ←→0 / 60

ゲームのバッグ

#046ミニチャレンジitems.erase(item)約 7 分

小さなゲームの 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)
main.cpp
#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;
}
Ctrl + Enter でも実行できます
結果

実行すると例で試せます。よさそうなら提出しましょう。