Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157667
  • 博文数量: 76
  • 博客积分: 1513
  • 博客等级: 上尉
  • 技术积分: 755
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-25 15:15
文章分类

全部博文(76)

文章存档

2012年(2)

2011年(74)

我的朋友

分类: C/C++

2011-11-25 21:20:51

  1. #include<iostream>

  2. #include<string>

  3. #include<vector>

  4. using namespace std;

  5.  

  6. template <typename Init,typename T>

  7. Init findElem(Init first,Init last,const T& val)

  8. {

  9.      while(first!=last)

  10.      {

  11.                    if(*first==val)

  12.                                   return first;

  13.                    first++;

  14.      }

  15.      return last;

  16.      

  17. }

  18.  

  19. int main()

  20. {

  21.     int a[]={2,4,5,7};

  22.     string s[]={"a","good","C++","test!"};

  23.     vector<int> ivec(a,a+4);

  24.     vector<string> svec(s,s+4);

  25.     

  26.     vector<int>::iterator iit=findElem(ivec.begin(),ivec.end(),4);

  27.     

  28.     if(iit!=ivec.end())

  29.        cout<<"found the element:"<<*iit<<endl;

  30.     else

  31.        cout<<"not found"<<endl;

  32.        

  33.     vector<string>::iterator sit;

  34.     sit=findElem(svec.begin(),svec.end(),"good");

  35.         if(sit!=svec.end())

  36.        cout<<"found the element:"<<*sit<<endl;

  37.     else

  38.        cout<<"not found"<<endl;

  39.     getchar();

  40.     return 0;

  41. }

--------------------------------------------------------------------------------------------------------------------------------------

---------------------------- ****************************** 输出***************************** ---------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------

Output:

found the element:4

found the element:good

阅读(584) | 评论(0) | 转发(0) |
0

上一篇:size_type 和 size_t 的区别

下一篇:引用

给主人留下些什么吧!~~