在结构和输出名称中查找最高值
Find highest value in struct and output name
我在下面有一个结构,希望打印出每只猫、狗和兔子的最大主人的名字。我该如何找到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只猫吗?
相关文章:
- 如何使用 do while 循环确定最高值和最低值
- 如何使用指针找到最高值?
- 在C++中为O(n)动态查找中值而不保存观测值
- 我无法显示包含整数最大值的索引. 但我可以显示数组中整数的最高值
- 如何在数组的一部分中查找特定值的第一个元素
- 应用函数后查找重复值的函数
- 在两个映射中查找共同值,无需迭代
- 在 c++ 中查找等于值到数组中
- 查找索引值在char表中
- 在 c++ 中查找中间值
- 从OpenCV MLP响应中获取最高值(概率)位置
- C++ 查找无序树中具有最高值的节点
- C++矩阵-获取列数,其中最低值与后面列中最高值位于同一行
- 用于选择一组具有最高值总和的间隔的算法
- 在数组C++中查找最高/最低元素时出错
- 我如何找出数组中的哪个元素具有最高值
- C++:在链表中查找类的最高值
- 在一个数组中查找最高分数,并将该索引与另一个数组的索引匹配
- 使用C++在两个排序数组中查找匹配值索引的最有效方法
- 在结构和输出名称中查找最高值