Auf einer gemeinsamen Reise wird jede Zahlung notiert, wer bezahlt hat und wie viel: names[i] hat amounts[i] bezahlt. Schreib spent_by(names, amounts), das eine map zurückgibt, wie viel jede Person insgesamt bezahlt hat. Beide vector sind gleich lang.
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++) {
// dein Code hier: zähl amounts[i] zur Summe von names[i] dazu
}
return total;
}
Ausführen testet die Beispiele. Wenn sie passen, drück auf Abgeben.