简体   繁体   English

有没有一种干净快速的方法来找到数组的最小值和最大值

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

Is there a clean and fast way to find the min and max of an array有没有一种干净快速的方法来找到数组的最小值和最大值

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

results are结果是

min = 2 / max = 22最小值 = 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) 
        
} 

Don't reinvent the wheel.不要重新发明轮子。 The C++ Standard Library exists to make common programming tasks take very little time, and with a very small chance of error. C++ 标准库的存在是为了使常见的编程任务花费很少的时间,并且出错的可能性很小。

You want std::min_element and std::max_element .你想要std::min_elementstd::max_element You'll need to include <algorithm> to use these.您需要包含<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));

The documentation for these functions can be found at the following pages:这些函数的文档可以在以下页面找到:

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

If you want to get really fancy you can use std::minmax with std::tie to unpack the std::pair returned.如果你想变得非常花哨,你可以使用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/algorithm/minmax_element
https://en.cppreference.com/w/cpp/utility/tuple/tie https://en.cppreference.com/w/cpp/utility/tuple/tie

You included an unfinished structural code snippet but without any apparent context in your question.您包含了一个未完成的结构代码片段,但在您的问题中没有任何明显的上下文。 I'm assuming this is your homework, where you're supposed to implement the logic.我假设这是你的作业,你应该在其中实现逻辑。

Based on that assumption I don't think using a library method such as.max() or.min() on the array/set will suffice as a solution.基于这个假设,我认为在数组/集合上使用诸如 .max() 或 .min() 之类的库方法不足以作为解决方案。 I would urge you to take a few minutes and,我建议你花几分钟时间,

  1. Think about the problem想想问题
  2. If you can, break the problem down into smaller problems如果可以,将问题分解成更小的问题
  3. Try and think up possible solutions, one small problem at a time试着想出可能的解决方案,一次一个小问题
  4. Try and implement your solution, step through with the debugger, see what happens.尝试并实施您的解决方案,逐步使用调试器,看看会发生什么。 If it fails, try other approaches, adjustments etc., maybe you'll get there!如果失败,尝试其他方法、调整等,也许你会成功!

If you manage to come up with a solution, great.如果你设法想出一个解决方案,那就太好了。 If not then either way I would recommend that after putting some mental effort on it googling possible solutions and discussions and explanations and really internalizing what is happening there.如果没有,那么无论哪种方式,我都建议付出一些精神努力之后,谷歌搜索可能的解决方案、讨论和解释,并真正内化那里发生的事情。

But here's pseudocode for you, because you're supposed to do your own homework.但这是给你的伪代码,因为你应该自己做作业。

    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

You can probably figure out how to find the minimum value from here if you use your brain.如果您动动脑筋,您可能会从这里想出如何找到最小值。

You can find the min, max and sum in one pass:您可以一次找到最小值、最大值和总和:

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;
}

The above is called a running maximum as well as a running minimum .以上称为运行最大值运行最小值 The sum is added at little bit of extra cost (not much).总和是在一点点额外成本(不多)的情况下添加的。

Thank you Thomas谢谢托马斯

This will used for linear position of multiple devices这将用于多个设备的线性 position

if one position gets to far off from another it will then run a Level function如果一个 position 离另一个很远,它将运行一个级别 function

Nice and simple漂亮而简单

  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