三个原因:
它更快:排序的容器,所有方法都受益于排序集合的快速对数搜索。此外,
std::string方法实现了最优算法。它更自然:
std::map和std::multimap方法可以直接搜索键,而不像算法必须查找std::pair<key,Value>,因为它们的迭代器可以直接指向。它在某些情况下更正确:在排序容器(如
map和set)中,所有方法都使用等价而不是相等,而某些算法(如std::count和std::find使用相等)则不是这样。
现在究如何把它应用到 STL 提供的各种容器来深入了解更多细节。
std::vector, std::deque, std::list 这些容器没有公开任何与搜索相关的方法,只能通过算法来搜索。
二、std::map/multimap, std::set/multiset
这些容器有5个类方法,它们跟一些算法共享它们的名称:count,find,equal_range,lower_bound和upper_bound。
这些方法和算法的比较:
容器方法 | 比算法更正确? | 比算法还快? | 比算法更自然? |
|---|---|---|---|
count | 是 | 是 | 是 |
find | 是 | 是 | 是 |
equal_range | 相同 | 是 | 是 |
lower_bound | 相同 | 是 | 是 |
upper_bound | 相同 | 是 | 是 |
更好的正确性是因为使用了等效而不是相等。
更好的性能来自于为序列容器对元素进行排序这一事实。对于关联容器来说,这是因为它们的迭代器不是随机访问的,所以算法不能通过直接跳过所需的元素来执行分割(它们必须从头开始并向上移动到它们的位置),而容器的内部没有这种约束。
它们对于映射来说更自然,因为传递给各种方法的参数是一个键,而不是
std::pair<key, Value>。
注意,没有跟std::binary_search等价的容器方法。检查容器中是否存在一个键:
对于
std::map和std::set:比较find的结果与end迭代器的结果。或者使用count方法。count作为方法不会引起任何性能问题,因为,像find一样,它在第一个与搜索的键相等的键处停止(因为根据std::map和std::set的定义,只能有一个键与搜索的键相等)。对于
std::multimap和std::multiset:因为count不会在第一个与搜索的键相等的键处停止,所以find在这里比count有优势。
在std::multimap或std::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组方法:
find 函数:用于从字符串中查找给定子字符串 str 的第一个匹配项。它返回子字符串在当前字符串中的位置索引,如果找不到则返回 std::string::npos。pos 参数是可选的,用于指定搜索的起始位置,默认为 0。
size_t find(const std::string& str, size_t pos = 0) const;
rfind 函数:与 find 函数类似,但它从字符串的末尾开始向前搜索子字符串 str 的最后一个匹配项。它返回子字符串在当前字符串中的位置索引,如果找不到则返回 std::string::npos。pos 参数是可选的,用于指定搜索的起始位置,默认为 std::string::npos,表示从末尾开始搜索。
size_t rfind(const std::string& str, size_t pos = npos) const;
find_first_of 函数:用于从字符串中查找与给定字符串 str 中的任何字符匹配的第一个字符。它返回找到的字符在当前字符串中的位置索引,如果找不到则返回 std::string::npos。pos 参数是可选的,用于指定搜索的起始位置,默认为 0。
size_t find_first_of(const std::string& str, size_t pos = 0) const;
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;
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;
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