Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
c++中如何使用std::find和std::find_if算法_c++查找算法find与find_if用法_创想鸟

c++中如何使用std::find和std::find_if算法_c++查找算法find与find_if用法

std::find用于查找等于指定值的元素,std::find_if用于查找满足条件的第一个元素,两者均返回迭代器,需与end()比较判断是否找到。

c++中如何使用std::find和std::find_if算法_c++查找算法find与find_if用法

在C++中,std::find 和 std::find_if 是定义在 gorithm> 头文件中的两个常用查找算法。它们用于在指定范围内搜索满足特定条件的元素,返回匹配元素的迭代器。如果未找到,则返回指向范围末尾的迭代器(即 end())。

std::find 的基本用法

std::find 用于在区间 [first, last) 中查找等于给定值的元素。

函数原型如下:

template
InputIt find(InputIt first, InputIt last, const T& value);

参数说明:

立即学习“C++免费学习笔记(深入)”;

first:起始迭代器 last:结束迭代器(不包含) value:要查找的值

示例代码:

#include iostream>
#include
#include

int main() {
std::vector vec = {10, 20, 30, 40, 50};
auto it = std::find(vec.begin(), vec.end(), 30);

if (it != vec.end()) {
std::cout } else {
std::cout }
return 0;
}

输出结果:

找到元素: 30

std::find_if 的基本用法

std::find_if 用于查找第一个满足指定条件的元素。条件由一个可调用对象(如函数指针、lambda 表达式或函数对象)定义。

函数原型:

template
InputIt find_if(InputIt first, InputIt last, UnaryPredicate p);

参数说明:

立即学习“C++免费学习笔记(深入)”;

first、last:搜索范围 p:一元谓词,返回 true 表示满足条件

示例:查找第一个偶数

#include stream>
#include
#include

int main() {
std::vector vec = {1, 3, 5, 8, 9, 10};
auto it = std::find_if(vec.begin(), vec.end(),
[](int x) { return x % 2 == 0; }); // lambda 判断是否为偶数

if (it != vec.end()) {
std::cout } else {
std::cout }
return 0;
}

输出结果:

第一个偶数是: 8

结合自定义结构体使用 find_if

当容器中存储的是结构体或类对象时,可以使用 std::find_if 配合 lambda 或函数对象进行复杂条件查找。

#include
#include
#include

struct Person {
std::string name;
int age;
};

int main() {
std::vector people = {
{“Alice”, 25},
{“Bob”, 30},
{“Charlie”, 35}
};

auto it = std::find_if(people.begin(), people.end(),
[](const Person& p) { return p.name == “Bob”; });

if (it != people.end()) {
std::cout name age } else {
std::cout }
return 0;
}

输出结果:

找到用户: Bob, 年龄: 30

基本上就这些。只要理解了迭代器范围和返回值的判断方式,再根据查找条件选择 find 或 find_if,就能高效完成常见查找任务。注意别忘了比较返回值是否等于 end() 来判断查找是否成功。

以上就是c++++中如何使用std::find和std::find_if算法_c++查找算法find与find_if用法的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1480502.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
c++怎么使用位集bitset_c++ bitset位操作用法详解
上一篇 2025年12月19日 04:41:40
c++中static_cast和dynamic_cast的区别_c++类型转换运算符对比解析
下一篇 2025年12月19日 04:41:52

相关推荐

发表回复

登录后才能评论
关注微信