2.10 寻找数组中的最大值和最小值
//vector #include <iostream> #include <vector> #include <time.h> using namespace std; void func1(int array[], int n,int &max,int&min) { max = array[0]; min = array[0]; // for(int i = 0;i<n;i++) { if(max<array[i]) max= array[i]; if(min>array[i]) min = array[i]; } } //C++向量的方式求解 void func2(vector<int>::iterator iter) { for (int i = 0;i<10;i++) { cout<<*iter<<' '; iter++; } } void main() { //vector<int>ivec; const int n = 10; int max,min; int a[n]; srand(time(0)); for(int i = 0;i<n;i++) { a[i] = rand(); cout<<a[i]<<' '; } cout<<endl; func1(a,10,max,min); vector<int>ivec(10,1); vector<int>::iterator iter; for(iter = ivec.begin(),i = 0;iter!= ivec.end(),i<n;iter++,i++) { *iter = a[i]; } iter = ivec.begin(); func2(iter); cout<<endl; cout<<max<<' '<<min<<endl; }