博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C++】search、search_n和find、find_if
阅读量:487 次
发布时间:2019-03-06

本文共 7169 字,大约阅读时间需要 23 分钟。

目录


search和search_n 

#include 
#include
#include
#include
#include
using namespace std;int main(){ array
test = {3,5,7,7,11,6,17,19}; array
t2 = {11,13}; array
::iterator it; //找到t2中元素在test中第一次出现的位置,不像find函数只能找一个 it=search(test.begin(),test.end(),t2.begin(),t2.end()); //寻找首次连续出现2次7的位置 it=search_n(test.begin(),test.end(),2,7); //寻找首次连续出现2次大于7的位置 it=search_n(test.begin(),test.end(),2,7,[](int i,int j){return i>j;}); if(it!=test.end())cout<

原文:

 

find和find_if

 

      回顾学习find和find_if, 网上查了一下资料,这里记录一下。

 需 #include <algorithm>

 

1、find

using namespace std;int main(){    list
lst;    lst.push_back(10);    lst.push_back(20);    lst.push_back(30);    list
::iterator it = find(lst.begin(), lst.end(), 10); // 查找list中是否有元素“10”    if (it != lst.end()) // 找到了    {        // do something     }    else // 没找到    {        // do something    }    return 0;}

那么,如果容器里的元素是一个类呢?要查找类,那么就需要重载==,拥有给find调用判断类是否相等

例如,有list<CPerson> ,其中CPerson类定义如下:

class CPerson{public:    CPerson(void);     ~CPerson(void);    bool CPerson::operator==(const CPerson &rhs) const    {        return (age == rhs.age);    }public:    int age; // 年龄};

    用find()函数进行查找,我们需要提供一个判断两个CPerson对象“相等”的定义,这个“相等”的定义,是通过重载“==”操作符实现的,我们在CPerson类中添加一个方法,定义为:

bool operator==(const CPerson &rhs) const;      实现为:bool CPerson::operator==(const CPerson &rhs) const{    return (age == rhs.age);}

然后我们就可以这样查找(假设list中已经有了若干CPerson对象)了:

list
lst;// 向lst中添加元素,此处省略CPerson cp_to_find; // 要查找的对象cp_to_find.age = 50;list
::iterator it = find(list.begin(), list.end(), cp_to_find); // 查找if (it != lst.end()) // 找到了{}else // 没找到{}这样就实现了需求。

2、find_if

若要找出 满足自己自定义的某些条件的 对象 则需要find_if 函数。

例如,有一个list<CPerson*>,这个list中的每一个元素都是一个对象的指针,我们要在这个list中查找具有指定age的元素,找到的话就得到对象的指针。

      这时候,你不再能像上面的例子那样做,我们需要用到find_if函数,并自己指定predicate function(即find_if函数的第三个参数,比较函数)。先看看find_if函数的定义:

template
InputIterator find_if(InputIterator _First, InputIterator _Last, Predicate _Pred);Parameters_FirstAn input iterator addressing the position of the first element in the range to be searched._Last    An input iterator addressing the position one past the final element in the range to be searched._Pred    User-defined predicate function object that defines the condition to be satisfied by the element being searched for. A predicate takes single argument and returns true or false.

我们在CPerson类外部定义这样一个结构体:

typedef struct finder_t{    finder_t(int n) : age(n)     {     }     bool operator()(CPerson *p)  //仿函数    {         return (age == p->age);     }     int age;}finder_t;

然后就可以利用find_if函数来查找了:

找年纪等于指定年龄的对象:

list
lst;// 向lst中添加元素,此处省略list
::iterator it = find_if(lst.begin(), lst.end(), finder_t(50)); // 查找年龄为50的人if (it != lst.end()) // 找到了{    cout << "Found person with age : " << (*it)->age;}else // 没找到{    // do something}

2.1、例子1

找key==0的对象:

map
mapItems;auto it = find_if(mapItems.begin(), mapItems.end(), [&](const pair
&item) {return item->first == 0/*期望值*/;});

2.2、例子2
 

找 .a > 2 && .b < 8 的对象

typedef struct testStruct{    int a;    int b;}testStruct;vector
testStructVector;auto itrFind = find_if(testStructVector.begin(), testStructVector.end(), [](testStruct myStruct){     return myStruct.a > 2 && myStruct.b < 8;}); if(itrFind != testStructVector.end())        TRACE("found!");else        TRACE("not found!");

2.3、例子3

 

#include 
#include
class map_value_finder{public:    map_value_finder(const std::string &cmp_string):m_s_cmp_string(cmp_string){}    bool operator ()(const std::map
::value_type &pair)    {        return pair.second == m_s_cmp_string;    }private:    const std::string &m_s_cmp_string;                    }; int main(){    std::map
my_map;    my_map.insert(std::make_pair(10, "china"));    my_map.insert(std::make_pair(20, "usa"));    my_map.insert(std::make_pair(30, "english"));    my_map.insert(std::make_pair(40, "hongkong"));            std::map
::iterator it = my_map.end();    it = std::find_if(my_map.begin(), my_map.end(), map_value_finder("English"));    if (it == my_map.end())       printf("not found\n");           else       printf("found key:%d value:%s\n", it->first, it->second.c_str());           return 0;        }

2.4、例子4

struct map_value_finder  {  public:      map_value_finder(const std::string &cmp_string):m_s_cmp_string(cmp_string){}      bool operator ()(const std::map
::value_type &pair)      {          return pair.second == m_s_cmp_string;      }  private:      const std::string &m_s_cmp_string;                      };   bool funddd(const std::map
::value_type &pair)  {      return pair.second == "english";  }   int main()  {      std::map
my_map;      my_map.insert(std::make_pair(10, "china"));      my_map.insert(std::make_pair(20, "usa"));      my_map.insert(std::make_pair(30, "english"));      my_map.insert(std::make_pair(40, "hongkong"));                std::map
::iterator it = my_map.end();      it = std::find_if(my_map.begin(), my_map.end(), funddd);      if (it == my_map.end())         printf("not found\n");             else         printf("found key:%d value:%s\n", it->first, it->second.c_str());      getchar();      return 0;          }  

下面再说绑定器bind:

      STL中的绑定器有类绑定器和函数绑定器两种,类绑定器有binder1st和binder2nd,而函数绑定器是bind1st和bind2nd,他们的基本目的都是用于构造一个一元的函数对象。比如这里我们可以利用bind2nd通过绑定二元函数对象中的第二个参数的方式来实现二元谓词向一元谓词的转换。

struct compare: binary_function<A, string,bool> {

    bool operator()( A &value, string str) const
    {
        if (value.GetStr()== str)
            return true;
        else
            return false;
    }
};

示例:

vector<A>::iterator t=find_if(a.begin(),a.end(),bind2nd(compare(), ”33″));

    无论是用vector的循环还是find_if泛型算法,在性能和代码复杂度上面都有一定得权衡,至于在实际应用中,还是需要具体问题具体分析的。

以下泛型模板

现在还是迷糊的,下面是自己在项目中看到的师傅写的一个比较实用的方法:

template<typename T> bool compare_no(const T* s1 , const T* s2)

{  
    return strcmp(s1->no, s2->no) == 0;
}

template<typename T> bool less_no(const T* s1 , const T* s2)

{
    return strcmp(s1->no, s2->no) < 0;
}

template<typename T> bool compare_id(const T* s1 , const T* s2)

{
    return s1->id == s2->id;
}

template<typename T> bool less_id(const T* s1 , const T* s2)

{
    return s1->id < s2->id;
}

//排序

std::sort(vct_device.begin(), vct_device.end(), less_id<ST_DEVICE>);
std::sort(vct_camer.begin(), vct_camer.end(), less_no<ST_CAMERA>);

//通过编号查找ID

vector<ST_CAMERA*>::iterator it_cam;

ST_CAMERA tmp_cam;
strcpy(tmp_cam.no, "888888");
it_cam = std::find_if(vct_camer.begin(),vct_camer.end(),bind2nd(ptr_fun(compare_no<ST_CAMERA>), &tmp_cam));
if (it_cam != vct_camer.end())
     返回值channel = (*it_cam)->channel;

 

//通过ID查找编号

vector<ST_CAMERA*>::iterator it_cam;

ST_CAMERA tmp_cam;
int camid = 0;
tmp_cam.id = 3;
it_cam = std::find_if(vct_camer_secd.begin(), vct_camer_secd.end(), bind2nd(ptr_fun(compare_id<ST_CAMERA>), &tmp_cam));
if (it_cam == vct_camer_secd.end())
       返回值strcpy(camera,(*it_cam)->no);
 

https://blog.csdn.net/bobodem/article/details/49386131

————————————————
版权声明:本文为CSDN博主「zzhongcy」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zzhongcy/article/details/87709685

你可能感兴趣的文章