标准库之find()和find_if()函数

标准库之find和find_if函数

find()

C++标准库中的find,find_if函数可以用于对数组、容器等进行查找。
http://www.cplusplus.com/reference/algorithm/find/

头文件

 #include <algorithm>

声明

template <class InputIterator, class T>
   InputIterator find (InputIterator first, InputIterator last, const T& val);
//实现
template<class InputIterator, class T>
  InputIterator find (InputIterator first, InputIterator last, const T& val)
{
  while (first!=last) {
    if (*first==val) return first;
    ++first;
  }
  return last;
}

find函数在[first,last)范围内,查找是否有值为val的位置,val必须要能支持operator==。

所以对于容器中的元素是基本类型时,可以直接使用find函数。
例如

#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
int main()
{
    list<int> lst{1,2,3,4};
    list<int>::iterator it = find(lst.begin(), lst.end(), 2); // 查找list中是否有元素“2”
    if (it != lst.end()) // 找到了
    {
        cout<<"find it"<<endl;
    }
    else // 没找到
    {
        cout<<"not find"<<endl;
    }
    system("pause");
    return 0;
}

如果容器里的元素是一个类,则需要对该类重载“==”操作符,然后在用find查找,
例如

#include<iostream>
#include<algorithm>
#include<list>
using namespace std;

class Person
{
public:
    Person(string name):m_name(name){}
    ~Person(void){}

public:
    string m_name; // 姓名

bool operator==(const Person& rhs) const{
    return (m_name==rhs.m_name);
}
};

int main()
{
    list<Person> lst{Person("Mike"),Person("John"),Person("Bobby")};

    Person one("John");
    list<Person>::iterator it = find(lst.begin(), lst.end(), one); // 查找list中是否有元素one

    if (it != lst.end()) // 找到了
    {
        cout<<"find it"<<endl;
    }
    else // 没找到
    {
        cout<<"not find"<<endl;
    }
    system("pause");
    return 0;
}

find_if()

还有一种情况是,容器中是一个指向类的指针,那怎么查找呢?
注意我们想比较的是指针指向的对象是否相等,而不是指针本身。

例如list<Person*>,这个list中的每一个元素都是一个对象的指针,我们要在这个list中查找是否存在该指针指向的对象与给定对象相等(当然,这里对象相等由你自定义其含义),找到就返回指向该对象的指针。

这时候,不能再使用find函数了,因为比较的是list<Person*>的元素Person*,即它比较的是指针是否相等,这往往不是我们想要的。

这里,我们需要使用find_if函数
参考:http://www.cplusplus.com/reference/algorithm/find_if/

头文件

 #include <algorithm>

定义

template <class InputIterator, class UnaryPredicate>
   InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
   
//实现
template<class InputIterator, class UnaryPredicate>
  InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  while (first!=last) {
    if (pred(*first)) return first;
    ++first;
  }
  return last;
}

find_if在[first,last)范围内查找能使得pred返回true的值,并返回指向该值的迭代器。

所以我们先在外面定义一个 的函数对象fun,如下

//函数对象
typedef struct fun{
    fun(string name):m_name(name){};
    bool operator()(Person* rhs)const {
        return (m_name==rhs->m_name);
    }
private:
    string m_name;
}fun;

然后利用find_if()函数查找,

#include<iostream>
#include<algorithm>
#include<list>
using namespace std;

class Person
{
public:
    Person(string name):m_name(name){}
    ~Person(void){}

public:
    string m_name; // 姓名
};

//函数对象
typedef struct fun{
    fun(string name):m_name(name){};
    bool operator()(Person* rhs)const {
        return (m_name==rhs->m_name);
    }
private:
    string m_name;
}fun;

int main()
{
    list<Person*> lst{new Person("Mike"),new Person("John"),new Person("Bobby")};

    list<Person*>::iterator it = find_if(lst.begin(), lst.end(), fun("John")); // 查找list中是否有元素能够使得fun(*it)返回true

    if (it != lst.end()) // 找到了
    {
        cout<<"find it"<<endl;
    }
    else // 没找到
    {
        cout<<"not find"<<endl;
    }

    //释放内存
    for(auto& i:lst){
        delete i;
        i=nullptr;
    }

    system("pause");
    return 0;
}

在这里插入图片描述

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值