Die Klasse wählt ein Klassentier, und votes enthält, wie viele Stimmen jedes Tier bekommen hat. Schreib top_pet(votes), das das Tier mit den meisten Stimmen zurückgibt. Bei Gleichstand nimm das, das alphabetisch zuerst kommt; hat niemand abgestimmt (leer), gib "" zurück.
top_pet({{"cat", 12}, {"dog", 15}, {"fish", 3}})→'dog'top_pet({})→''string top_pet(const map<string, int> &votes)#include <map>
#include <string>
using namespace std;
string top_pet(const map<string, int> &votes) {
string best = "";
int most = -1;
for (const auto &p : votes) {
// dein Code hier: hat es mehr als most, merk es dir
}
return best;
}
Ausführen testet die Beispiele. Wenn sie passen, drück auf Abgeben.