C++中*max_element(v.begin,v.end)找最大元素*min_element(v.begin,v.end)找最小元素。
数组:
#include<iostream> #include<algorithm> using namespace std; int main() { int a[]={0,1,2,3,4,5,6,7,8,9,10}; int len=sizeof(a)/sizeof(int); cout<< *max_element(a,a+len)<<endl; cout<< *min_element(a,a+len)<<endl; return 0; }容器:
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int n; vector<int>vec; while(cin>>n){ int a; for(int it=0;it<n;it++){ cin>>a; vec.push_back(a); } cout<<*max_element(vec.begin(), vec.end())<<endl; cout<<*min_element(vec.begin(), vec.end())<<endl; } return 0;感觉很好用哦。。。。。