C++ STL之min_element()与max_element()(取容器中的最大最小值)

min_element()和max_element

头文件:#include<algorithm>

作用:返回容器中最小值和最大值。max_element(first,end,cmp);其中cmp为可选择参数!

 

闲言少叙,上代码,一看就懂:

 

#include<iostream>  
#include<algorithm>  
using namespace std;  
bool cmp(int a,int b)  
{  
      return a<b;  
}  
int main()  
{  
      int num[]={2,3,1,6,4,5};  
      cout<<"最小值是 "<<*min_element(num,num+6)<<endl;  
      cout<<"最大值是 "<<*max_element(num,num+6)<<endl;  
      cout<<"最小值是 "<<*min_element(num,num+6,cmp)<<endl;  
      cout<<"最大值是 "<<*max_element(num,num+6,cmp)<<endl;  
      return 0;   
}

转载自:https://www.cnblogs.com/ECJTUACM-873284962/p/6734225.html

相关文章:

  • 2023-02-12
  • 2021-06-17
  • 2021-10-05
  • 2022-01-22
  • 2021-11-29
  • 2021-06-01
猜你喜欢
  • 2021-06-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案