[英]C++ Loop Through Array, Find Smallest
I'm trying to find the smallest value in 3 different array but it's not turning out right I don't think. 我正在尝试在3个不同的数组中找到最小值,但我认为并没有得出正确的结果。 It never seems to return the middle array even if it is the smallest.
即使它是最小的,它似乎也永远不会返回中间数组。 It's always either 0 or 2. What seems to be my logic error?
始终为0或2。似乎是我的逻辑错误?
int smallest;
for(int i = 1; i < 3; i++)
{
if(queue[i].getCount() < queue[0].getCount())
smallest = i;
else
smallest = 0;
}
Given the errors in your code, it's a little difficult to see what you are trying to do. 鉴于代码中的错误,要查看要执行的操作有点困难。
I would think you want something more like this: 我认为您想要更多这样的东西:
int smallest = queue[0].getCount();
for(int i = 1; i < 3; i++)
{
if(queue[i].getCount() < smallest)
smallest = queue[i].getCount();
}
If you instead want the resulting index, try something like this: 如果您想要结果索引,请尝试如下操作:
int smallest = 0;
for(int i = 1; i < 3; i++)
{
if(queue[i].getCount() < queue[smallest].getCount())
smallest = i;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.