クラスで飼うペットを投票で決めます。votes には各ペットの票数が入っています。一番票が多いペットを返す top_pet(votes) を書きましょう。同票ならアルファベット順で先のものを選び、誰も投票していない(空)なら "" を返します。
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) {
// ここにコードを書く:most より多ければ覚えておく
}
return best;
}
実行すると例で試せます。よさそうなら提出しましょう。