总结:find_if针对查找的对象中包含指针需要进行比较

find则更偏向于普通的数值或者字符比较

两者都可以应用于自定义的类,只需在类中重载==运载符

函数调用符()说白了其实就是代替函数指针,调用对应重载的()的那个定义函数,()运算符只能在类中重载

STL的find,find_if函数提供了一种对数组、STL容器进行查找的方法。使用该函数,需包含头文件 #include <algorithm>
我们查找一个list中的数据,通常用find(),例如:

那么,如果容器里的元素是一个类呢?例如,有list<CPerson> ,其中CPerson类定义如下:

class CPerson
{
public:
    CPerson(void); 
    ~CPerson(void);

public:
    int age; // 年龄
};

那么如何用find()函数进行查找呢?这时,我们需要提供一个判断两个CPerson对象“相等”的定义,find()函数才能从一个list中找到与指定的CPerson“相等”的元素。
这个“相等”的定义,是通过重载“==”操作符实现的,我们在CPerson类中添加一个方法,定义为:
bool operator==(const CPerson &rhs) const;
实现为:
bool CPerson::operator==(const CPerson &rhs) const
{
    return (age == rhs.age);
}

然后我们就可以这样查找(假设list中已经有了若干CPerson对象)了:
list<CPerson> lst;
//////////////////////////////////
// 向lst中添加元素,此处省略
//////////////////////////////////
CPerson cp_to_find; // 要查找的对象
cp_to_find.age = 50;
list<CPerson>::iterator it = find(list.begin(), list.end(), cp_to_find); // 查找

if (it != lst.end()) // 找到了
{
    // do something 
}
else // 没找到
{
    // do something
}
这样就实现了需求。

有人说,如果我有自己定义的“相等”呢?例如,有一个list<CPerson*>,这个list中的每一个元素都是一个对象的指针,我们要在这个list中查找具有指定age的元素,找到的话就   得到   对象的指针。  不只是普通需要比较类型上的相等,而是两个对象的指针所对应的值进行比较
这时候,你不再能像上面的例子那样做,

我们需要用到find_if函数,并自己指定predicate function谓词函数(即find_if函数的第三个参数,请查阅STL手册)。先看看find_if函数的定义:
template<class InputIterator, class Predicate>
InputIterator find_if(InputIterator _First, InputIterator _Last, Predicate _Pred);
Parameters
_First
An 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.

在这顺带说明一下()的函数调用符号()的重载,它只能通过类的成员来重载

 

在main()函数中,cc是一个类,但是”cc();”这样的语法却是函数调用,在项目中这样的写法可以避免代码出现函数指针

说白了,()其实就是函数指针的作用,代替了函数指针而已,在C++之后的java,就不存在指针了

我们在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<CPerson*> lst;
//////////////////////////////////
// 向lst中添加元素,此处省略
//////////////////////////////////

list<CPerson*>::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

引自https://blog.csdn.net/CNHK1225/article/details/48678203

C++中find_if的更多相关文章

  1. 泛型2(lambda表达式/参数绑定)

    lambda 表达式: Lambda表达式完整的声明格式如下: [capture list] (params list) mutable exception-> return type { fu ...

  2. Python开源框架

    info:更多Django信息url:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC) ...

  3. STL中的find_if函数

      上一篇文章也讲过,find()函数只能处理简单类型的内容,也就是缺省类型,如果你想用一个自定义类型的数据作为查找依据则会出错!这里将讲述另外一个函数find_if()的用法 这是find()的一个 ...

  4. STL 查找vector容器中的指定对象:find()与find_if()算法

    1 从vector容器中查找指定对象:find()算法 STL的通用算法find()和find_if()可以查找指定对象,参数1,即首iterator指着开始的位置,参数2,即次iterator指着停 ...

  5. C++中的vector&find_if

     <STL應用> vector & find_if 看到有人問有個名為C的struct如下 code: struct C { int v1; int v2; }; 應用在vecto ...

  6. 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)

    使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element ...

  7. c++ 查找容器中符合条件的元素,并返回iterator(find_if)

    #include <iostream> // std::cout #include <algorithm> // std::find_if #include <vecto ...

  8. 记录 C++ STL 中 一些好用的函数--持续更新 (for_each,transform,count_if,find_if)

    在日常的编程中,有这么几种操作还是比较常见的: 把一组数据都赋值成一个数,在一组数据中查找一个数,统计一组数据中符合条件的数等等. 一般的写法可以用循环,没有什么是循环不能搞定的.假如在这里怎么用介绍 ...

  9. C++中vector的用法

    C++内置的数组支持容器的机制,但是它不支持容器抽象的语义.要解决此问题我们自己实现这样的类.在标准C++中,用容器向量(vector)实现.容器向量也是一个类模板.标准库vector类型使用需要的头 ...

随机推荐

  1. UI简单工作

     UI用户界面 需求——效果图——风格设计——高保证效果——html 网页的宽度=屏幕的宽度-纵向滚动条的宽度 企业网站一般是1280 根据百度流量研究所 目前我们的网页注主要是1024和1200   ...

  2. python 2.X 和 3.X 的区别汇总

    python 2.x                                                     python 3.x print() 或者 print “abc”都可以  ...

  3. 2018面向对象程序设计(Java)第8周学习指导及要求

    2018面向对象程序设计(Java)第8周学习指导及要求 (2018.10.18-2018.10.21)   学习目标 掌握接口定义方法: 掌握实现接口类的定义要求: 掌握实现了接口类的使用要求: 理 ...

  4. oracle_效率优化

    1.并行和强制走索引的用法 SELECT/*+parallel(T 16) parallel(B 16) parallel(D 16)*/ T.POLICY_NO, T.DEPARTMENT_CODE ...

  5. Lattice Constants and Crystal Structures of some Semiconductors

    Lattice Constants and Crystal Structures of some Semiconductors and Other Materials Element or Compo ...

  6. gitlab-ce 安装、汉化与阿里邮箱配置(注意是CE)

    环境准备 yum install curl openssh-server openssh-clients postfix cronie policycoreutils-python –y curl h ...

  7. c++三种进制格式

    来源:c++ primer plus 常用的进制有二进制,八进制,十进制,十六进制,在c++的头文件iostream里除了提供了endl控制符之外,还提供了控制进制的控制符,(不含二进制),分别是八进 ...

  8. NoHtml

    private string NoHtml(string Htmlstring) { if (string.IsNullOrWhiteSpace(Htmlstring)) return string. ...

  9. HDU 6118 度度熊的交易计划(最小费用最大流)

    Problem Description度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个 ...

  10. Springboot学习02-webjars和静态资源映射规则

    Springboot学习01-webjars和静态资源映射规则 前言 1-以前我们在IDEA中创建一个项目,添加web依赖包,我们现在是一个web应用,应该在man目录下面有一个webapp文件夹,将 ...