在结构和输出名称中查找最高值

Find highest value in struct and output name

本文关键字:查找 最高值 结构 输出      更新时间:2023-10-16

我在下面有一个结构,希望打印出每只猫、狗和兔子的最大主人的名字。我该如何找到3个最大的,并打印每个最大的所有者?我有300个有3只动物的主人。

struct Animals
{
    string ownername;
    int cats;
    int dogs;
    int rabbits;
};
struct World
{
    Animals number[MAXANIMALS];
} myAnimalWorld;

这看起来真的很琐碎。在阵列上循环一次,跟踪最大兔子、最大猫和最大狗的数量和主人。

我继续使用类似的东西来获得它。

int max = a[0];
for(int i = 0; i < n; ++i)
{
    if(max < a[i]) {
        max = a[i];
    }
}

使用std::max_element和自定义比较器来满足每个需求。

好奇心-为什么类型是double?你希望有人养2.5只猫吗?