簡體   English   中英

有沒有一種干凈快速的方法來找到數組的最小值和最大值

[英]Is there a clean and fast way to find the min and max of an array

有沒有一種干凈快速的方法來找到數組的最小值和最大值

int array1[] = {2,6,10,22,12};

結果是

最小值 = 2 / 最大值 = 22

// Function  
void min_max(set<int> my_set) 
{ 
    for (auto i : my_set); 
} 

// Function to find the maximum element 
int findMax(set<int> my_set) 
{ 

    // Get the maximum element 
    int max_element; 
    if (!my_set.empty()) 
        max_element = *(my_set.rbegin()); 

    // return the maximum element 
    return max_element; 
} 

// Function to find the minimum element 
int findMin(set<int> my_set) 
{ 

    // Get the minimum element 
    int min_element; 
    if (!my_set.empty()) 
        min_element = *my_set.begin(); 

    // return the minimum element 
    return min_element; 
} 

int main() 
{ 

    // Get the set 
    set<int> my_set = {2,6,10,22,12}; 
        // results
        findMin(my_set) 
        findMax(my_set) 
        
} 

不要重新發明輪子。 C++ 標准庫的存在是為了使常見的編程任務花費很少的時間,並且出錯的可能性很小。

你想要std::min_elementstd::max_element 您需要包含<algorithm>才能使用它們。

#include <algorithm> // for min/max_element
#include <iterator>  // for begin/end

...

int array1[] = {2,6,10,22,12};

int min = std::min_element(std::begin(array1), std::end(array1));
int max = std::max_element(std::begin(array1), std::end(array1));

這些函數的文檔可以在以下頁面找到:

https://en.cppreference.com/w/cpp/algorithm/min_element
https://en.cppreference.com/w/cpp/algorithm/max_element
https://en.cppreference.com/w/cpp/iterator/begin

如果你想變得非常花哨,你可以使用std::minmaxstd::tie來解壓返回的std::pair

#include <algorithm> // for minmax_element
#include <iterator>  // for begin/end
#include <tuple>     // for tie

...

int array1[] = {2,6,10,22,12};

int min, max;
std::tie(min, max) = std::minmax_element(std::begin(array1), std::end(array1));

https://en.cppreference.com/w/cpp/algorithm/minmax_element
https://en.cppreference.com/w/cpp/utility/tuple/tie

您包含了一個未完成的結構代碼片段,但在您的問題中沒有任何明顯的上下文。 我假設這是你的作業,你應該在其中實現邏輯。

基於這個假設,我認為在數組/集合上使用諸如 .max() 或 .min() 之類的庫方法不足以作為解決方案。 我建議你花幾分鍾時間,

  1. 想想問題
  2. 如果可以,將問題分解成更小的問題
  3. 試着想出可能的解決方案,一次一個小問題
  4. 嘗試並實施您的解決方案,逐步使用調試器,看看會發生什么。 如果失敗,嘗試其他方法、調整等,也許你會成功!

如果你設法想出一個解決方案,那就太好了。 如果沒有,那么無論哪種方式,我都建議付出一些精神努力之后,谷歌搜索可能的解決方案、討論和解釋,並真正內化那里發生的事情。

但這是給你的偽代碼,因為你應該自己做作業。

    let maxValue = minimum value of whatever datatype you're using

    for each number in some array
      if the number is larger than maxValue then 
        maxValue = number
        
    return maxValue

如果您動動腦筋,您可能會從這里想出如何找到最小值。

您可以一次找到最小值、最大值和總和:

int array1[] = {2,6,10,22,12};
const int quantity = 
    sizeof(array1) / sizeof(array1[0]);
int max = array1[0];
int min = array1[0];
int sum = 0;
for (int i = 1; i < quantity; ++i)
{
  const int value = array1[i];
  if (value > max) max = value;
  if (value < min) min = value;
  sum = sum + value;
}

以上稱為運行最大值運行最小值 總和是在一點點額外成本(不多)的情況下添加的。

謝謝托馬斯

這將用於多個設備的線性 position

如果一個 position 離另一個很遠,它將運行一個級別 function

漂亮而簡單

  const int quantity = 
  sizeof(adj_str) / sizeof(adj_str[0]); // analog read of multiple linear position's 
  int max = adj_str[0];
  int min = adj_str[0];
  //int sum = 0;
    for (int i = 1; i < quantity; ++i)
      {
       const int value = adj_str[i];
       if (value > max) max = value;
       if (value < min) min = value;
       //sum = sum + value; 
      }
         int dwn_max = (max);
         int up_min = (min);
         int adj_str_dif = dwn_max - up_min; // example = if adj_str_dif > 6 then run level();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM