【问题标题】:My If Else statement is not working to provide maximum value in a vector (C++)我的 If Else 语句无法在向量中提供最大值(C++)
【发布时间】:2018-10-11 14:39:17
【问题描述】:

我正在尝试返回最大值。在 C++ 中的向量中的值,但我一直只得到最后一个值作为响应(我猜这是因为 if-else 循环在某种程度上没有进行比较,只是将下一个值分配给“maxVal”)。例如,下面的代码返回 30 作为答案。我究竟做错了什么?你能帮忙吗?

下面是代码->

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

double max (const vector<double>& myVector)   {
int n = myVector.size();
double maxVal;
for (int i=0; i<=n-1; i++) {
    if (maxVal <= myVector[i+1])  {
        maxVal = myVector[i+1];   
    }
    else {
        maxVal = myVector[i];
    }

}
return maxVal; 
}

int main() {
vector<double> testVector;
testVector.push_back(10.0);
testVector.push_back(200.0);
testVector.push_back(30.0);
cout << max(testVector);
return 0;

}

【问题讨论】:

  • 通常i &lt;= n-1 会写成i &lt; n。如果n 是像std::size_t 这样的无符号整数,这实际上很重要。
  • 在每次迭代中,您都将maxVal 的值设置为当前元素或下一个元素。因此,您正在计算 最后两个向量元素的最大值。
  • 请注意,i+1 在最后一次迭代中将超出向量的范围。
  • maxVal 未在函数 max () 中初始化。使用 double maxVal = myVector[0];
  • 对不起,这是处理这种情况的错误方法:我建议您放置调试行(cout 或其他),并显示您的值 MaxVal 的进度,我, MyVector[i+1], ...,看看它们是如何演变的。像这样,您将了解问题所在以及如何处理。

标签: c++ loops for-loop if-statement


【解决方案1】:

C++ 有一个丰富的库,我不明白为什么人们不使用它。 这是一个找到向量最大值的两行版本。请不要重新发明轮子。

#include <iostream>                                                                                                                                                                                                
#include <vector>                                                                                                                                                                                                  
#include <algorithm>                                                                                                                                                                                                

int main()                                                                                                                                                                                                         
{                                                                                                                                                                                                                  
  auto v = std::vector{4, 3, 2, 1};                                                                                                                                                                               
  std::cout << *max_element(v.cbegin(), v.cend()) << "\n";                                                                                                                                                         
}                                                                                                                                                                                                                  

【讨论】:

    【解决方案2】:

    (我假设向量不为空)

    这项工作:

    double max(const std::vector<double>& myVector)
    {
        size_t n = myVector.size();
        double maxVal = myVector[0];
    
        for (size_t i = 0; i < n; i++)
        {
            if (myVector[i] > maxVal)
                maxVal = myVector[i];
        }
    
        return maxVal;
    }
    
    int main()
    {
        std::vector<double> testVector{ 10.0 , 200.0 , 30.0 };
    
        std::cout << max(testVector);
    
        return 0;
    }
    

    保持简单易读:用向量的第一个元素初始化 maxVal,在所有元素上运行,如果有一个大于 maxVal,则更新 maxVal。

    【讨论】:

      【解决方案3】:

      使用标准算法让事情变得轻松自在。

      见https://en.cppreference.com/w/cpp/algorithm/max_element

      【讨论】:

        【解决方案4】:

        这里

        for (int i=0; i<=n-1; i++) {
            if (maxVal <= myVector[i+1])  {
                maxVal = myVector[i+1];   
            }
            else {
                maxVal = myVector[i];
            }
        }
        

        您在每次迭代时更改maxVal 的值。最后 maxVal 只能是最后一个元素的值或数组后面的元素。这让我想到了下一个问题:有效索引是 0 到(包括)n-1,所以当你在 i==n-1 时,i+1 不在你的数组中。

        实际上不需要考虑下一个或上一个元素,因此将其更改为

        for (int i=0; i<n; i++) {        // loop from 0 till n-1 
            if (maxVal < myVector[i]) {  // no need to check equality
                maxVal = myVector[i];   
            }                            // no need for else
        }                                // if maxVal is bigger then it 
                                         // is already the max so far
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-11-23
          • 2016-10-13
          • 1970-01-01
          • 1970-01-01
          • 2013-04-21
          • 2018-01-27
          • 1970-01-01
          相关资源
          最近更新 更多