Using `std :: search` over` string :: find`

I have a question about using std::searchvs string::findto work with strings. I know that it is often better to use the function algorithm of a particular class for a standard library algorithm, because it can be optimized based on the class, but I was wondering if it is reasonable, for the sake of consistency, to use std::searchiterators, not string::findindexes.

Would it be a sin for me to do something like this, or should I just stick to the :: find line? Are there any huge advantages of one over the other in terms of performance or style?

+6
source share
2 answers

(27 2017 ), , GCC libstdc++ ( clang ) std::string::find , , ,

std::string_view substr{"whatever"};
auto it = std::search(s.cbegin(), s.cend(),
                      std::boyer_moore_searcher(substr.begin(), substr.end())); 

, Boyer-Moore , , std::bad_alloc. , std::string::find noexcept, Boyer-Moore std::string::find .

+7

string:: find , , Boyer Moore ( ). libstd++, lib++, :: find . gcc (7.1), . , www.github.com/hiraditya/std-benchmark

, , , () :: find . , HTML .., , :: find .

commit fc7ebc4b8d9ad7e2891b7f72152e8a2b7543cd65
Author: redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Mon Jan 9 13:05:58 2017 +0000

    PR66414 optimize std::string::find

    2017-01-09  Jonathan Wakely  <jwakely@redhat.com>
            Aditya Kumar  <hiraditya@msn.com>

        PR libstdc++/66414
        * include/bits/basic_string.tcc
        (basic_string::find(const CharT*, size_type, size_type)): Optimize.

    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@244225 138bc75d-0d04-0410-961f-82ee72b054a4

PS: std:: find std::string:: find .

+3

Source: https://habr.com/ru/post/1016824/


All Articles