#include <iostream>
#include <vector>
using namespace std;
/*
bool findElement(vector::iterator beg,
vector::iterator end,
int elem)
{
for(vector::iterator ivec = beg; ivec != end; ivec++)
{
if(elem == *ivec)
{
return true;
}
}
return false;
}*/
vector<int>::iterator findElement(vector<int>::iterator beg,
vector<int>::iterator end,
int ival)
{
while(beg != end)
{
if(*beg ==ival)
break;
else
++beg;
}
return beg;
}
int main(int argc,char *argv[])
{
// bool flag;
int ia[] = {0,1,2,3,4,5,6,7,8,9};
vector<int> ivec(ia,ia+10);
/* vector::iterator ibeg = ivec.begin();
vector::iterator iend = ivec.end();
flag = findElement(ibeg,iend,6);
if(true == flag)
{
cout<<"Find element!"<
}
else
{
cout<<"Didn't find element!"<
}*/
/*读入要找的数据*/
cout<<"Please input a integer:";
int ival;
cin>>ival;
// cout<
/*调用findElement函数查找ival*/
vector<int>::iterator it;
it = findElement(ivec.begin(),ivec.end(),ival);
if(it != ivec.end())
cout<<ival<<" is a element of the vector."<<endl;
else
cout<<ival<<" isn't a element of the vector."<<endl;
return 0;
}
|