c++的STL算法库find怎么用 在容器中查找指定元素【实用教程】
技术百科
尼克
发布时间:2026-01-01
浏览: 次 std::find 是 C++ STL 中用于线性查找第一个匹配元素的算法,需包含 头文件,接受起始迭代器、结束迭代器和待查值三个参数,返回指向匹配元素的迭代器或 end()。
find 是 C++ STL 中最常用的查找算法之一,它在指定范围内线性搜索第一个匹配的元素,返回指向该元素的迭代器;如果没找到,就返回范围末尾的迭代器(通常是 end())。
基本用法:需要头文件和三个参数
使用 std::find 前要包含 头文件。它接受三个参数:
- 起始迭代器(如
v.begin()) - 结束迭代器(如
v.end(),注意不包含该位置) - 要查找的值(类型需与容器元素可比较)
例如,在 vector 中找数字 7:
#include
#include
#include
std::vectorv = {1, 5, 7, 3, 7, 9};
auto it = std::find(v.begin(), v.end(), 7);
if (it != v.end()) {
std::cout << "找到,索引为:" << (it - v.begin()) << std::endl;
} else {
std::cout << "未找到" << std::endl;
支持所有支持迭代器的容器
不只是 vector,find 可用于 list、deque、array、string,甚至原生数组(配合指针):
std::string s = "hello"; auto it = std::find(s.begin(), s.end(), 'l');int arr[] = {2,4,6}; auto it = std::find(std::begin(arr), std::end(arr), 4);
注意:map 和 unordered_map 不适合直接用 find 查 value(效率低),它们自带更高效的 find() 成员函数查 key。
自定义类型查找:必须支持 == 比较
若容器存的是自定义结构体或类,需重载 operator==,否则编译失败:
struct Person {
std::string name;
int age;
bool operator==(const Person& other) const {
return name == other.name && age == other.age;
}
};
std::vector people = {{"Alice", 30}, {"Bob", 25}};
auto it = std::find(people.begin(), people.end(), Person{"Bob", 25}); 找不到时返回 end(),务必检查再解引用
这是常见错误来源:直接对返回的迭代器取值而不判断是否有效,会导致未定义行为:
- ✅ 正确:
if (it != container.end()) { use *it; } - ❌ 危险:
std::cout (万一没找到)
对 string 或 vector 等连续容器,也可用 it - begin() 得到下标;但对 list 这类链表,只能用 std::distance(begin(), it) 计算位置。
# ai
# 的是
# 这类
# 它在
# 这是
# 第一个
# 而不
# 自定义
# 迭代
# 不适合
# auto
# go
# c++
# String
# if
# int
# 指针
# stream
# red
# operator
# 成员函数
# 结构体
# 算法
# 头文件
# Struct
# map
# ios
# const
# bool
# include
# Array
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- 如何关闭Win10自动更新更新_Win10系统自动
- Win11怎样安装企业微信_Win11安装企业微信
- Win11怎么设置按流量计费_Win11限制后台流
- c++怎么处理多线程死锁_c++ lock_gua
- php错误怎么开启_display_errors与
- 如何使用Golang实现跨域请求支持_Golang
- Win11怎么设置ipv4地址_Windows 1
- Python脚本参数接收_sys与argparse
- Win10怎样安装PPT模板_Win10安装PPT
- Win11如何设置文件权限 Win11 NTFS文
- Win10如何卸载微软拼音输入法 Win10只保留
- Python解释执行模型_字节码流程说明【指导】
- windows如何修改文件默认打开方式_windo
- Windows10怎样连接蓝牙设备_Windows
- 如何使用Golang实现容器健康检查_监控和自动重
- MAC怎么用连续互通相机里的“桌上视角”_MAC在
- 如何在Golang中写入XML文件_生成符合规范的
- php中::能访问全局变量吗_全局作用域与类作用域
- Win11笔记本怎么看电池健康度_Win11电池报
- 如何在 Go 中创建包含 map 的 slice(
- Python 中将 ISO 8601 时间戳转换为
- Python异步编程高级项目教程_asyncio协
- Python并发安全问题_资源竞争说明【指导】
- Win11时间格式怎么改成12小时制 Win11时
- Python代码测试策略_质量保障解析【教程】
- php485函数执行慢怎么优化_php485性能提
- Win10系统怎么查看网络连接状态_Windows
- Windows10系统怎么查看IP地址_Win10
- php8.4如何配置ssl证书_php8.4htt
- 如何使用Golang sort排序切片_Golan
- VSC怎么配置PHP的Xdebug_远程调试设置步
- 如何使用Golang实现文件加密_Golang c
- Windows10系统怎么查看设备管理器_Win1
- Go 中实现 Python urllib.quot
- Win11键盘快捷键大全_Windows 11常用
- Windows 11如何开启文件夹加密(EFS)_
- Windows 11无法安全删除U盘提示设备正在使
- PhpStorm怎么调试PHP代码_PhpStor
- Win11时间怎么同步到原子钟 Win11高精度时
- php本地部署后session无法保存_sessi
- 如何理解Go指针和内存分配关系_Go Pointe
- Dapper的Execute方法的返回值是什么意思
- Win10怎样卸载DockerDesktop_Wi
- Windows服务无法启动错误1067是什么_进程
- Mac怎么安装软件_Mac安装dmg与pkg文件的
- 如何用正则表达式精确匹配“start”到“end”
- Win11怎么禁用键盘自带键盘_Win11笔记本禁
- Windows10蓝屏SYSTEM_SERVICE
- php控制舵机角度怎么调_php发送pwm信号控制
- 如何在 Go 中正确反序列化多个同级 XML 元素


QQ客服