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

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

那么,如果容器里的元素是一个类呢?例如,有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 (id == 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.

我们在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

最新文章

  1. 必会Redis单节点、Sentinel和Cluster操作实战
  2. WebSocket相关
  3. windows无法完成安装 若要在此计算机上安装_全新安装Windows10 2004的技巧,官方工具,安全高效...
  4. C++ Primer 第十六章 模板与范型编程
  5. 【VMCloud云平台】SCVMM配置(四)创建模板机准备
  6. Python与自然语言处理搭建环境
  7. 服务器维护需要log日志,什么是服务器日志?服务器日志要怎么看?
  8. DataTable 深入解析数据源绑定原理之高级篇
  9. python数据分析系统_Python数据分析,系统步骤介绍!
  10. 给定数组,查找最小的k个元素或最大的k个元素
  11. matlab晶闸管整流电路,三相桥式全控整流电路 MATLAB/SIMULINK电力电子电路仿真
  12. 如何用十步写一首原创歌曲
  13. JAVA将图片背景色设置为透明
  14. 什么是MBR/DPT/DBR/BPB?
  15. 成为软件架构师需要什么?
  16. 快来喝杯Java(初级第一章)
  17. AIDL简单实用新手教程(AIDL 包含回调,耗时处理,in out inout oneway使用、打包jar等内容) 附demo下载
  18. 飞浆AI studio人工智能课程学习(2)-Prompt优化思路|十个技巧高效优化Prompt|迭代法|Trick法|通用法|工具辅助
  19. 解决SVN Can’t open file ‘/XXX/xxx/db/txn-current-lock’错误
  20. Gitea 无法启动提示 (code=exited, status=203/exec) 错误

热门文章

  1. 双击图片不放大手机php,Android_Android App中实现可以双击放大和缩小图片功能的实例,先来看一个很简单的核心图片 - phpStudy...
  2. 怎么python安装mysql库_python在windows上怎么安装mysql数据库
  3. matlab中inv a,设A为矩阵,b为列向量,则Matlab中运算A\b 和运算inv(A)*b
  4. python爬虫html、parser_利用python HTMLParser标准库实现一个简单的爬虫
  5. idea中修改git账号和密码
  6. MySQL(11)-----多表创建及描述表关系(多对多的分析和实现)
  7. grub.conf解析
  8. 分享:EditText默认不弹出软件键盘
  9. HTML5离线缓存(Application Cache)
  10. 关于TCP/UDP缓存