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

クラスのペット投票

#033基礎for (const auto &p : votes)約 5 分

クラスで飼うペットを投票で決めます。votes には各ペットの票数が入っています。一番票が多いペットを返す top_pet(votes) を書きましょう。同票ならアルファベット順で先のものを選び、誰も投票していない(空)なら "" を返します。

例
top_pet({{"cat", 12}, {"dog", 15}, {"fish", 3}})→'dog'
top_pet({})→''
関数string top_pet(const map<string, int> &votes)
main.cpp
#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) {
        // ここにコードを書く:most より多ければ覚えておく
    }
    return best;
}
Ctrl + Enter でも実行できます
結果

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