博客
关于我
【C++】search、search_n和find、find_if
阅读量:488 次
发布时间:2019-03-06

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

C++中的find和find_if函数教程

Element

findfind_if是C++标准库中用于容器中的元素查找的强大工具。find用于查找特定的值,而find_if用于查找满足自定义条件的元素。两者都使用迭代器进行操作,对于支持random-access iterator的容器(如vector和list)效率更高。

1. find

std::find函数用于查找具有指定值的元素。以下是一个基本的例子:

#include 
#include
using namespace std;int main() { vector
lst; lst.push_back(10); lst.push_back(20); lst.push_back(30); iterator
it = find(lst.begin(), lst.end(), 10); if (it != lst.end()) { // 查找成功 } else { // 未找到 } return 0;}

当查找的对象是类对象时,可以通过重载==操作符来实现比较。例如:

class CPerson {public:    CPerson() {}    ~CPerson() {}    bool operator==(const CPerson &rhs) const {        return this->age == rhs.age;    }    int age;};list
lst;CPerson cp_to_find;cp_to_find.age = 50;iterator
it = find(lst.begin(), lst.end(), cp_to_find);

2. find_if

find_if函数用于查找满足特定条件的元素。第三个参数是一个谓词函数,返回bool值。

#include 
#include
using namespace std;class CPerson {public: CPerson() {} ~CPerson() {} bool operator==(const CPerson &rhs) const { return this->age == rhs.age; } int age;};typedef struct finder_t { finder_t(int n) : age(n) {} bool operator()(const CPerson *p) { return p->age == age; } int age;};int main() { list
lst; iterator
it = find_if(lst.begin(), lst.end(), finder_t(50));}

3. 绑定器(Binder)

在C++中,bind函数用于将二元谓词转换为一元谓词。可以使用bind2nd将一个函数的一部分参数进行绑定,常用于实现自定义查找逻辑。

#include 
#include
#include
using namespace std;struct testStruct { int a; int b;};vector
testStructVector;auto itrFind = find_if(testStructVector.begin(), testStructVector.end(), [](testStruct myStruct) { return myStruct.a > 2 && myStruct.b < 8; });

4. 高级使用方法

以下是通过自定义函数执行查找的示例:

#include 
#include
using namespace std;class map_value_finder {public: map_value_finder(const string &cmp_string) : m_s_cmp_string(cmp_string) {} bool operator()(const pair
&pair) { return pair.second == m_s_cmp_string; }private: string m_s_cmp_string;};int main() { map
my_map; my_map.insert(make_pair(10, "china")); my_map.insert(make_pair(20, "usa")); my_map.insert(make_pair(30, "english")); my_map.insert(make_pair(40, "hongkong")); iterator
> it = find_if(my_map.begin(), my_map.end(), map_value_finder("English"));}

5. 绑定器示例

以下是一个使用绑定器的示例,展示了如何将一个比较函数绑定到find_if上:

#include 
#include
#include
using namespace std;struct compare { bool operator()(A value, string str) const { return value.GetStr() == str; }};vector
a;auto t = find_if(a.begin(), a.end(), bind2nd(compare(), "33"));

结论

std::findstd::find_if函数是C++标准库中的实力强大的工具,能够满足大多数的元素查找需求。理解并正确使用这些函数的区别和优点,将有助于提高代码的效率和简洁性。

转载地址:http://hqndz.baihongyu.com/

你可能感兴趣的文章
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>