小编Eri*_*c V的帖子

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

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

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)

c++ arrays

-4
推荐指数
1
解决办法
1559
查看次数

标签 统计

arrays ×1

c++ ×1