Kodewick
学びはじめる →
基礎←→0 / 60

旅行の費用

#029基礎total[names[i]] += …約 4 分

みんなで旅行に行き、支払いのたびに誰がいくら払ったかを記録しました。names[i] が amounts[i] を払っています。一人ひとりが払った合計を map で返す spent_by(names, amounts) を書きましょう。2 つの vector は同じ長さです。

例
spent_by({"Amy", "Bao", "Amy"}, {30, 12, 8})→{'Amy': 38, 'Bao': 12}
spent_by({}, {})→{}
関数map<string, int> spent_by(const vector<string> &names, const vector<int> &amounts)
main.cpp
#include <map>
#include <string>
#include <vector>
using namespace std;

map<string, int> spent_by(const vector<string> &names, const vector<int> &amounts) {
    map<string, int> total;
    for (int i = 0; i < names.size(); i++) {
        // ここにコードを書く:names[i] の合計に amounts[i] を足す
    }
    return total;
}
Ctrl + Enter でも実行できます
結果

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