有没有一种干净快速的方法来查找数组的最小值和最大值
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();
// …Run Code Online (Sandbox Code Playgroud)