Kodewick
Start learning →
Basic←→0 / 60

Trip expenses

#029BasicMapsvector4 min

On a trip together, each payment is written down as who paid and how much: names[i] paid amounts[i]. Write spent_by(names, amounts) to return a map of how much each person paid in all. Both vectors are the same length.

Examples
spent_by({"Amy", "Bao", "Amy"}, {30, 12, 8})→{'Amy': 38, 'Bao': 12}
spent_by({}, {})→{}
The functionmap<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++) {
        // your code here: add amounts[i] to names[i]'s total
    }
    return total;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.