news 2026/3/23 3:55:30

掌握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/3/15 14:49:57

NCM格式解密终极指南:简单方法实现网易云音乐文件转换

NCM格式解密终极指南&#xff1a;简单方法实现网易云音乐文件转换 【免费下载链接】ncmdump 项目地址: https://gitcode.com/gh_mirrors/ncmd/ncmdump 还在为网易云音乐的NCM加密文件无法在其他设备播放而烦恼吗&#xff1f;&#x1f914; 今天我们就来分享一个完整的解…

作者头像 李华
网站建设 2026/3/22 8:18:21

WELearnHelper终极学习助手:一键解锁自动答题新体验

WELearnHelper终极学习助手&#xff1a;一键解锁自动答题新体验 【免费下载链接】WELearnHelper 显示WE Learn随行课堂题目答案&#xff1b;支持班级测试&#xff1b;自动答题&#xff1b;刷时长&#xff1b;基于生成式AI(ChatGPT)的答案生成 项目地址: https://gitcode.com/…

作者头像 李华
网站建设 2026/3/21 4:53:20

数据建模终极指南:5分钟快速上手实体关系图

数据建模终极指南&#xff1a;5分钟快速上手实体关系图 【免费下载链接】mermaid 项目地址: https://gitcode.com/gh_mirrors/mer/mermaid 还在为复杂的数据库设计发愁吗&#xff1f;每次开会讨论表结构都像在猜谜语&#xff1f;实体关系图&#xff08;ER图&#xff09…

作者头像 李华
网站建设 2026/3/16 12:53:55

纪念币预约终极神器:5分钟快速配置全自动抢币工具

纪念币预约终极神器&#xff1a;5分钟快速配置全自动抢币工具 【免费下载链接】auto_commemorative_coin_booking 项目地址: https://gitcode.com/gh_mirrors/au/auto_commemorative_coin_booking 还在为纪念币预约抢不到而烦恼吗&#xff1f;这款纪念币预约自动化工具…

作者头像 李华
网站建设 2026/3/15 14:57:25

ResNet18实战教程:工业视觉检测应用开发

ResNet18实战教程&#xff1a;工业视觉检测应用开发 1. 引言 1.1 工业视觉检测的现实挑战 在智能制造、自动化质检和安防监控等工业场景中&#xff0c;传统基于规则的图像处理方法&#xff08;如边缘检测、模板匹配&#xff09;已难以应对复杂多变的环境。光照变化、目标形变…

作者头像 李华