#include <iostream>
#include <vector>
using namespace std;
/*
bool findElement(vector<int>::iterator beg,
vector<int>::iterator end,
int elem)
{
for(vector<int>::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<int>::iterator ibeg = ivec.begin();
vector<int>::iterator iend = ivec.end();
flag = findElement(ibeg,iend,6);
if(true == flag)
{
cout<<"Find element!"<<endl;
}
else
{
cout<<"Didn't find element!"<<endl;
}*/
/*读入要找的数据*/
cout<<"Please input a integer:";
int ival;
cin>>ival;
// cout<<endl;
/*调用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;
}
|