C++ find()函数用法(一般用于vector的查找)

相信学习C++的人有很多人用过CString.find()函数,但是你有么有用过 std::find() 函数呢?

       find函数主要实现的是在容器内查找指定的元素,并且这个元素必须是基本数据类型的。
查找成功返回一个指向指定元素的迭代器,查找失败返回end迭代器。

例一,在数组中查找:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# include  <iostream>
# include  <vector>
# include  <algorithm> //注意要包含该头文件
using  namespace  std;
int  main()
{
     int  nums[] = {  3 1 4 1 5 9  };
     int  num_to_find =  5 ;
     int  start =  0 ;
     int  end =  5 ;
     int * result = find( nums + start, nums + end, num_to_find );
     if ( result == nums + end ) 
     {
         cout<<  "Did not find any number matching "  << num_to_find << endl;
    
     else
     {
          cout<<  "Found a matching number: "  << *result << endl;
     }
     return  0 ;
}

 运行结果如下:


例二,在容器中查找:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
#include <algorithm>
using  namespace  std;
int  main(){
         vector< int > v;
         int  num_to_find=25; //要查找的元素,类型要与vector<>类型一致
         for ( int  i=0;i<10;i++)
                 v.push_back(i*i);
         vector< int >::iterator iter=std::find(v.begin(),v.end(),num_to_find); //返回的是一个迭代器指针
         if (iter==v.end())
             cout<< "ERROR!" <<endl;
         else                //注意迭代器指针输出元素的方式和distance用法
             cout<< "the index of value " <<(*iter)<< " is "  << std::distance(v.begin(), iter)<<std::endl;
         return  0;
}

运行结果如下:


另外还有一个函数find_if

find_if函数 带条件的查找元素,容器元素类型是类的时候,不能使用find函数,只能通过find_if函数来实现。find_if函数依次的遍历容器的元素,返回第一个使函数为true的元素的迭代器,如果查找失败则返回end迭代器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include <algorithm>
using  namespace  std;
template < typename  T>
bool  equal_3(T value){
         return  value==3;
}
int  main(){
         vector< int > vec;
         vec.push_back(7);
         vec.push_back(3);
         vec.push_back(8);
         vector< int >::iterator finda=find_if(vec.begin(),vec.end(),equal_3< int >);
         if (finda!=vec.end())
                 cout<< "YES" <<*finda<<endl;
         else
                 cout<< "ERROR" <<endl;
         return  0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
struct  Point
{
     int  x;
     int  y;
};
struct  PointFindByCoord :  public  std::binary_function<Point, Point,  bool >
{
     bool  operator () ( const  Point &obj1,  const  Point &obj2)  const
     {
         return  obj1.x == obj2.x && obj1.y == obj2.y;
     }
};
int  main()
{
     std::vector<Point> v;
     for  ( int  i = 0; i < 5; ++i)
     {
         for  ( int  j = 0; j < 5; ++j)
         {
             Point pt;
             pt.x = i;
             pt.y = j;
             v.push_back(pt);
         }
     }
     Point needFind;
     needFind.x = 4;
     needFind.y = 3;
     std::vector<Point>::iterator iter=std::find_if(v.begin(),v.end(),std::bind2nd(PointFindByCoord(), needFind));
     if  (iter == v.end())
     {
         // 未找到  
     }
     else
         std::cout <<  "the index of value Point("  << (*iter).x <<  ", "  << (*iter).y
             <<  ") is "  << std::distance(v.begin(), iter) << std::endl;
    
     return  0;
}


  • 41
    点赞
  • 225
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值