みんなで旅行に行き、支払いのたびに誰がいくら払ったかを記録しました。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)#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;
}
実行すると例で試せます。よさそうなら提出しましょう。