【问题标题】:easiest way to know the position of minimum values in array c++了解数组c ++中最小值位置的最简单方法
【发布时间】:2020-10-24 19:42:28
【问题描述】:

我有这段代码,我怎样才能找到读数数组中的最小值位置。 例如,如果最小值是读数 [10],有没有办法我可以编写代码

int count = 0;
long readings[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for (count = 0;count <21;count++;){
readings [count] =  time2[count] + (time3[count] * 60) + ( time4[count] * 3600);
}

我实际上是在像这样使用 if 语句。 if ( (readings[0] &lt;= readings[1]) &amp;&amp; (readings[2]) ) {} 但现在我想计算并比较数组中的 20 个数字(读数 [0])和(读数 [20]),如果有一种方法可以让我知道数组中的最小元素位置,我将不胜感激。

【问题讨论】:

  • 您正在尝试使用 for 循环初始化数组的每个元素,现在您想获取数组中最小元素的位置?
  • 嗨,尝试使用标准容器(std::array、std::vector(如果大小不固定))。这些容器有实现的方法。 en.cppreference.com/w/cpp/algorithm/min_element
  • @Celz - 标准算法也适用于原始数组。 std::min_element(std::begin(readings), std::end(readings)) 将给出一个指向最小元素的指针(或第一个找到的元素,如果多个元素具有相同的最小值)。 std::distance(std::begin(readings), std::min_element(std::begin(readings), std::end(readings))) 会将其转换为(有符号的)整数索引。
  • 对于 OP - 从 count = 0 循环到 count &lt; 21 意味着您的代码写到了 readings 的末尾,因此会导致未定义的行为。循环到count &lt; 20。我认为for 中额外的; 是一个错字。
  • @Peter 很高兴知道,谢谢

标签: c++ arrays position minimum


【解决方案1】:

std::min_element 将为您完成工作:

auto index = std::distance(std::begin(readings), std::min_element(std::begin(readings), std::end(readings)));

【讨论】:

    【解决方案2】:

    在我看来,下面的 sn-p 是你应该继续的方式。

    tmp_inx = 0;
    for(int i = 1; i < 20; i++) {
        if(readings[i]<readings[tmp_inx]){
            tmp_inx = i;
        }
    }
    

    【讨论】:

      【解决方案3】:

      是的,只需遍历数组并保存找到最小值的位置。像这样的

      long readings[20] = ...;
      
      long min_reading = readings[0];
      int min_position = 0;
      for (int i = 1; i < 20; ++i)
      {
          if (readings[i] < min_reading)
          {
              min_reading = readings[i];
              min_position = i; // save minimum position
          }
      }
      cout << "the minimum position is " << min_position << endl;
      

      【讨论】:

      • int i = 1?不应该是零吗?
      • 否,因为您已经考虑了 for 循环之前两行中的零读数。
      【解决方案4】:

      你可以用这个方法找到沿着最小数组位置的最小元素:

      #include <iostream>
      
      int main(void) {
          // an array example
          long longNumbers[] = {3, 4444, 32, 55, 1, 93, 4};
      
          // getting the array length
          size_t len = sizeof(longNumbers) / sizeof(longNumbers[0]);
      
          // intentionally declared to be used for position
          int pos = 0;
      
          // assuming the first index as the smallest initially
          int smallest = longNumbers[pos];
      
          // iterating till 'len'
          for (size_t i = 1; i < len; i++)
              // if smallest is still greater than some value
              // then deduce it
              if (smallest > longNumbers[i]) {
                  smallest = longNumbers[i];
                  pos = i;
              }
          
          std::cout << "The smallest element " << smallest
                    << " exists in index: " << pos << std::endl;
          
          return 0;
      }
      

      它会给出如下输出:

      The smallest element 1 exists in index: 4
      

      表示整个数组集合中最小的元素是1,它位于索引位置4

      【讨论】:

      • @john 怎么样?这正确显示了最小元素和数组索引,并且我在编译期间使用了-Wall 标志,也没有报告任何警告。
      • 您的代码仅在最小数字前面是按降序排序的数字(如您的示例中)时才有效,否则它将返回不正确的索引,例如godbolt.org/z/-h5H4N
      • @AlanBirtles 答案已更新。现在它似乎工作正常。
      • @RohanBari 代码仍然不正确,现在您有loc 计算不等于最小元素的元素数量。对我来说,当您需要某物的索引时使用基于范围的循环的整个想法是错误的。
      • @john 那么也许我们需要使用 STL 算法或简单的 for 循环。
      猜你喜欢
      • 2011-05-20
      • 2016-04-25
      • 1970-01-01
      • 2013-03-22
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多