news 2026/4/16 19:56:55

掌握C++ STL容器搜索技巧:实现高效和准确的数据访问

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
掌握C++ STL容器搜索技巧:实现高效和准确的数据访问

三个原因:

  • 它更快:排序的容器,所有方法都受益于排序集合的快速对数搜索。此外,std::string方法实现了最优算法。

  • 它更自然:std::mapstd::multimap方法可以直接搜索键,而不像算法必须查找std::pair<key,Value>,因为它们的迭代器可以直接指向。

  • 它在某些情况下更正确:在排序容器(如mapset)中,所有方法都使用等价而不是相等,而某些算法(如std::countstd::find使用相等)则不是这样。

现在究如何把它应用到 STL 提供的各种容器来深入了解更多细节。

std::vector, std::deque, std::list 这些容器没有公开任何与搜索相关的方法,只能通过算法来搜索。

二、std::map/multimap, std::set/multiset

这些容器有5个类方法,它们跟一些算法共享它们的名称:count,find,equal_range,lower_boundupper_bound

这些方法和算法的比较:

容器方法

比算法更正确?

比算法还快?

比算法更自然?

count

find

equal_range

相同

lower_bound

相同

upper_bound

相同

  • 更好的正确性是因为使用了等效而不是相等。

  • 更好的性能来自于为序列容器对元素进行排序这一事实。对于关联容器来说,这是因为它们的迭代器不是随机访问的,所以算法不能通过直接跳过所需的元素来执行分割(它们必须从头开始并向上移动到它们的位置),而容器的内部没有这种约束。

  • 它们对于映射来说更自然,因为传递给各种方法的参数是一个键,而不是std::pair<key, Value>

注意,没有跟std::binary_search等价的容器方法。检查容器中是否存在一个键:

  • 对于std::mapstd::set:比较find的结果与end迭代器的结果。或者使用count方法。count作为方法不会引起任何性能问题,因为,像find一样,它在第一个与搜索的键相等的键处停止(因为根据std::mapstd::set的定义,只能有一个键与搜索的键相等)。

  • 对于std::multimapstd::multiset:因为count不会在第一个与搜索的键相等的键处停止,所以find在这里比count有优势。

std::multimapstd::multiset中,find方法返回与搜索值相等的任何元素,而不一定是第一个元素。如果确实需要第一个元素,可以使用equal_range,因为它具有简单的接口;或者,如果觉得equal_range太慢(因为它显示了整个范围),而只是需要第一个元素,那么可以使用lower_bound。但是,lower_bound同样也有它的缺点,也必须付出代价。

三、std::string

string实际上有24个搜索方法。分成6组,每组有4个重载。对于所有组,4个重载的形式为:

  • 搜索由std::string给出的字符串。

  • 搜索由char*和size给出的字符串。

  • 搜索由char*给出的字符串(止于null字符)。

  • 搜索一个字符。

并且所有4个重载都以搜索字符串中的起始位置作为参数,默认值为0(从字符串的开头开始搜索)。

以下是6组方法:

  1. find 函数:用于从字符串中查找给定子字符串 str 的第一个匹配项。它返回子字符串在当前字符串中的位置索引,如果找不到则返回 std::string::npos。pos 参数是可选的,用于指定搜索的起始位置,默认为 0。

    size_t find(const std::string& str, size_t pos = 0) const;

  2. rfind 函数:与 find 函数类似,但它从字符串的末尾开始向前搜索子字符串 str 的最后一个匹配项。它返回子字符串在当前字符串中的位置索引,如果找不到则返回 std::string::npos。pos 参数是可选的,用于指定搜索的起始位置,默认为 std::string::npos,表示从末尾开始搜索。

    size_t rfind(const std::string& str, size_t pos = npos) const;

  3. find_first_of 函数:用于从字符串中查找与给定字符串 str 中的任何字符匹配的第一个字符。它返回找到的字符在当前字符串中的位置索引,如果找不到则返回 std::string::npos。pos 参数是可选的,用于指定搜索的起始位置,默认为 0。

    size_t find_first_of(const std::string& str, size_t pos = 0) const;

  4. find_last_of 函数:与 find_first_of 函数类似,但它从字符串的末尾开始向前搜索与给定字符串 str 中的任何字符匹配的最后一个字符。它返回找到的字符在当前字符串中的位置索引,如果找不到则返回 std::string::npos。pos 参数是可选的,用于指定搜索的起始位置,默认为 std::string::npos,表示从末尾开始搜索。

    size_t find_last_of(const std::string& str, size_t pos = npos) const;

  5. find_first_not_of 函数:用于从字符串中查找第一个不在给定字符串 str 中的字符。它返回找到的字符在当前字符串中的位置索引,如果找不到则返回 std::string::npos。pos 参数是可选的,用于指定搜索的起始位置,默认为 0。

    size_t find_first_not_of(const std::string& str, size_t pos = 0) const;

  6. find_last_not_of 函数:与 find_first_not_of 函数类似,但它从字符串的末尾开始向前搜索第一个不在给定字符串 str 中的字符。它返回找到的字符在当前字符串中的位置索引,如果找不到则返回 std::string::npos。pos 参数是可选的,用于指定搜索的起始位置,默认为 std::string::npos,表示从末尾开始搜索。

    size_t find_last_not_of(const std::string& str, size_t pos = npos) const;

在线性时间内实现字符串算法并不容易。string方法以最佳方式实现它们,当在字符串中搜索某些内容时,可以直接使用容器的方法。

https://www.dongchedi.com/article/7594153635873882649
https://www.dongchedi.com/article/7594154845427827225
https://www.dongchedi.com/article/7594151436733514264
https://www.dongchedi.com/article/7594148355094708761
https://www.dongchedi.com/article/7594143059890602558
https://www.dongchedi.com/article/7594142952818426392
https://www.dongchedi.com/article/7594142964432323096
https://www.dongchedi.com/article/7594143059890209342
https://www.dongchedi.com/article/7594143001568707134
https://www.dongchedi.com/article/7594142838871376409
https://www.dongchedi.com/article/7594113284840669758
https://www.dongchedi.com/article/7594110260978238014
https://www.dongchedi.com/article/7594110220667028030
https://www.dongchedi.com/article/7594109748606665278
https://www.dongchedi.com/article/7594110099447513624
https://www.dongchedi.com/article/7594108451488334398
https://www.dongchedi.com/article/7594106177244561945
https://www.dongchedi.com/article/7594103370105979417
https://www.dongchedi.com/article/7594199071733596734
https://www.dongchedi.com/article/7594198145211761176
https://www.dongchedi.com/article/7594196964511154713
https://www.dongchedi.com/article/7594198239285494297
https://www.dongchedi.com/article/7594197322151068222
https://www.dongchedi.com/article/7594198145211925016
https://www.dongchedi.com/article/7594195638192243225
https://www.dongchedi.com/article/7594198041578455614
https://www.dongchedi.com/article/7594179233010893336
https://www.dongchedi.com/article/7594179607033922110
https://www.dongchedi.com/article/7594177906826625560
https://www.dongchedi.com/article/7594177530207289918
https://www.dongchedi.com/article/7594176033486864958
https://www.dongchedi.com/article/7594174514142462488
https://www.dongchedi.com/article/7594175199944213017
https://www.dongchedi.com/article/7594173003894964798
https://www.dongchedi.com/article/7594172411499954713
https://www.dongchedi.com/article/7594172274706514456
https://www.dongchedi.com/article/7594171609930383897
https://www.dongchedi.com/article/7594171501943931416
https://www.dongchedi.com/article/7594171895055286808
https://www.dongchedi.com/article/7594169899916853785
https://www.dongchedi.com/article/7594168856931762712
https://www.dongchedi.com/article/7594154845427630617
https://www.dongchedi.com/article/7594152696526504510
https://www.dongchedi.com/article/7594154096370991678
https://www.dongchedi.com/article/7594149478186533401
https://www.dongchedi.com/article/7594143476796015128
https://www.dongchedi.com/article/7594143420428370457
https://www.dongchedi.com/article/7594143309313147417
https://www.dongchedi.com/article/7594143309313081881
https://www.dongchedi.com/article/7594143555656942104
https://www.dongchedi.com/article/7594143559859765822
https://www.dongchedi.com/article/7594112718668349977
https://www.dongchedi.com/article/7594111610630013464
https://www.dongchedi.com/article/7594112414933795353
https://www.dongchedi.com/article/7594110167684661784
https://www.dongchedi.com/article/7594108789133951550
https://www.dongchedi.com/article/7594110242670101016
https://www.dongchedi.com/article/7594108398888010302
https://www.dongchedi.com/article/7594198471734198809
https://www.dongchedi.com/article/7594199227367309886
https://www.dongchedi.com/article/7594196987835466264
https://www.dongchedi.com/article/7594196535337353752
https://www.dongchedi.com/article/7594197773559185944
https://www.dongchedi.com/article/7594196903693156888
https://www.dongchedi.com/article/7594197731099771416
https://www.dongchedi.com/article/7594196461773406745
https://www.dongchedi.com/article/7594180373895709246
https://www.dongchedi.com/article/7594178432767017497
https://www.dongchedi.com/article/7594177510213272088
https://www.dongchedi.com/article/7594177633563460121

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 14:26:59

PCL2-CE社区版启动器:打造你的专属Minecraft游戏管家

PCL2-CE社区版启动器&#xff1a;打造你的专属Minecraft游戏管家 【免费下载链接】PCL2-CE PCL2 社区版&#xff0c;可体验上游暂未合并的功能 项目地址: https://gitcode.com/gh_mirrors/pc/PCL2-CE 还在为繁琐的Minecraft启动流程而烦恼吗&#xff1f;想要一款既稳定又…

作者头像 李华
网站建设 2026/4/16 14:33:38

Minecraft NBT编辑器终极探索:从数据新手到修改大师的实战秘籍

Minecraft NBT编辑器终极探索&#xff1a;从数据新手到修改大师的实战秘籍 【免费下载链接】NBTExplorer A graphical NBT editor for all Minecraft NBT data sources 项目地址: https://gitcode.com/gh_mirrors/nb/NBTExplorer 你是否曾经因为Minecraft存档损坏而痛心…

作者头像 李华
网站建设 2026/4/16 14:33:41

新手必看:高速PCB仿真前处理操作指南

高速PCB仿真前处理&#xff1a;新手避坑指南与实战精要你有没有遇到过这样的情况&#xff1f;辛辛苦苦做完高速板子的布局布线&#xff0c;信心满满地跑完信号完整性&#xff08;SI&#xff09;仿真&#xff0c;结果眼图闭合、串扰超标。回头改了几处走线&#xff0c;问题依旧。…

作者头像 李华
网站建设 2026/4/16 14:04:07

碧蓝航线Alas:终极自动化管理完整指南

碧蓝航线Alas&#xff1a;终极自动化管理完整指南 【免费下载链接】AzurLaneAutoScript Azur Lane bot (CN/EN/JP/TW) 碧蓝航线脚本 | 无缝委托科研&#xff0c;全自动大世界 项目地址: https://gitcode.com/gh_mirrors/az/AzurLaneAutoScript 还在为重复的舰队管理任务…

作者头像 李华
网站建设 2026/4/16 14:32:42

ResNet18部署案例:智能工厂监控系统

ResNet18部署案例&#xff1a;智能工厂监控系统 1. 引言&#xff1a;通用物体识别在工业场景中的价值 随着智能制造的快速发展&#xff0c;智能工厂对实时、精准的视觉感知能力提出了更高要求。传统监控系统仅能实现“录像回溯”&#xff0c;而无法主动理解画面内容。引入基于…

作者头像 李华
网站建设 2026/4/16 14:29:36

深蓝词库转换终极指南:5步搞定20+输入法词库无缝迁移

深蓝词库转换终极指南&#xff1a;5步搞定20输入法词库无缝迁移 【免费下载链接】imewlconverter ”深蓝词库转换“ 一款开源免费的输入法词库转换程序 项目地址: https://gitcode.com/gh_mirrors/im/imewlconverter 还在为换输入法就要重新积累词库而烦恼吗&#xff1f…

作者头像 李华