- #include<iostream>
-
-
#include<string>
-
-
#include<vector>
-
-
using namespace std;
-
-
-
-
template <typename Init,typename T>
-
-
Init findElem(Init first,Init last,const T& val)
-
-
{
-
-
while(first!=last)
-
-
{
-
-
if(*first==val)
-
-
return first;
-
-
first++;
-
-
}
-
-
return last;
-
-
-
-
}
-
-
-
-
int main()
-
-
{
-
-
int a[]={2,4,5,7};
-
-
string s[]={"a","good","C++","test!"};
-
-
vector<int> ivec(a,a+4);
-
-
vector<string> svec(s,s+4);
-
-
-
-
vector<int>::iterator iit=findElem(ivec.begin(),ivec.end(),4);
-
-
-
-
if(iit!=ivec.end())
-
-
cout<<"found the element:"<<*iit<<endl;
-
-
else
-
-
cout<<"not found"<<endl;
-
-
-
-
vector<string>::iterator sit;
-
-
sit=findElem(svec.begin(),svec.end(),"good");
-
-
if(sit!=svec.end())
-
-
cout<<"found the element:"<<*sit<<endl;
-
-
else
-
-
cout<<"not found"<<endl;
-
-
getchar();
-
-
return 0;
-
-
}
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------- ****************************** 输出***************************** ---------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
Output:
found the element:4
found the element:good
阅读(615) | 评论(0) | 转发(0) |