用vector遍历的方法写一个函数,返回数组中的最大值。函数申明如下:int max(vect

分别用两种遍历方法写一个函数,返回数组中的最大值。函数申明如下:
int max(vector<int>ivec);

第1个回答  2010-04-16
方法一:
int max(vector<int> ivec) //与数组相似的方法
{
int temp=0;
for(int i=0;i<ivec.size();i++)
if(temp<ivec[i])
temp=ivec[i];
return temp;
}

方法二:
int max(vector<int> ivec) //使用遍历器的方法
{
int temp=0;
for(vector<int>::iterator it=ivec.begin();it!=ivec.end();it++)
{
if(temp<*it)
temp=*it;
}
return temp;
}