functional_boyer_moore_searcher

functional_boyer_moore_searcher (Boyer-Moore 검색기)

이 페이지에서는 C++17부터 사용할 수 있는 std::boyer_moore_searcher에 대해 설명해요. 이 클래스는 Boyer-Moore 문자열 검색 알고리즘을 구현한 검색기로, std::search의 Searcher 오버로드와 함께 사용할 수 있어요. 복사 생성과 복사 할당이 가능하며, 효율적인 부분 문자열 검색을 위해 내부 데이터 구조를 미리 구성해 둔답니다.

출처: cppreference

본문

Defined in header
template < class RandomIt1 , class Hash = std :: hash < typename std :: iterator_traits < RandomIt1 >:: value_type > , class BinaryPredicate = std :: equal_to <> > class boyer_moore_searcher ; (since C++17)

std::boyer_moore_searcherCopyConstructibleCopyAssignable 요구 사항을 만족해요. RandomIt1LegacyRandomAccessIterator 요구 사항을 충족해야 해요.

멤버 함수 (Member functions)

생성자 (Constructor)

boyer_moore_searcher ( RandomIt1 pat_first , RandomIt1 pat_last , Hash hf = Hash (), BinaryPredicate pred = BinaryPredicate () );

pat_first, pat_last, hf, pred의 복사본을 저장하고 필요한 내부 데이터 구조를 설정하여 std::boyer_moore_searcher를 생성해요.

RandomIt1의 값 타입은 DefaultConstructible, CopyConstructible, CopyAssignable이어야 해요.

std::iterator_traits<RandomIt1>::value_type 타입의 임의의 두 값 A와 B에 대해 pred(A, B) == true라면 hf(A) == hf(B)도 true여야 해요.

매개변수 (Parameters)
pat_first, pat_last - 검색할 문자열을 나타내는 반복자 쌍
hf - 문자열 요소를 해시하는 데 사용하는 호출 가능 객체
pred - 동등성을 판별하는 데 사용하는 호출 가능 객체
예외 (Exceptions)

다음에서 발생하는 예외는 모두 전달돼요:

  • RandomIt1의 복사 생성자;
  • RandomIt1 값 타입의 기본 생성자, 복사 생성자, 복사 할당 연산자; 또는
  • BinaryPredicateHash의 복사 생성자와 함수 호출 연산자.

내부 데이터 구조에 필요한 추가 메모리를 할당할 수 없으면 std::bad_alloc도 던질 수 있어요.

operator() (검색 연산자)

template < class RandomIt2 > std :: pair < RandomIt2 , RandomIt2 > operator ()( RandomIt2 first , RandomIt2 last ) const ; (since C++17)

이 멤버 함수는 std::search의 Searcher 오버로드가 이 검색기를 사용해 검색을 수행할 때 호출해요. RandomIt2LegacyRandomAccessIterator 요구 사항을 충족해야 하며, RandomIt1RandomIt2는 같은 값 타입을 가져야 해요.

매개변수 (Parameters)
first, last - 검색 대상 문자열을 나타내는 반복자 쌍
반환값 (Return value)

패턴 [pat_first, pat_last)이 비어 있으면 std::make_pair(first, first)를 반환해요. 그렇지 않으면 [first, last) 범위에서 pred가 정의하는 동등 비교로 [pat_first, pat_last)와 같은 부분 수열이 위치한 첫 번째와 마지막 다음 위치를 가리키는 반복자 쌍을 반환해요. 찾지 못하면 std::make_pair(last, last)를 반환해요.

참고 사항 (Notes)

Feature-test macro Value Std Feature
__cpp_lib_boyer_moore_searcher 201603L (C++17) searchers

예제 (Example)

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string_view>

int main()
{
    constexpr std::string_view haystack =
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
        "do eiusmod tempor incididunt ut labore et dolore magna aliqua";
    
    const std::string_view needle{"pisci"};
    
    if (const auto it = std::search(haystack.begin(), haystack.end(),
            std::boyer_moore_searcher(needle.begin(), needle.end()));
        it != haystack.end()
    )
        std::cout << "The string " << std::quoted(needle) << " found at offset "
                  << it - haystack.begin() << '\n';
    else
        std::cout << "The string " << std::quoted(needle) << " not found\n";
}

Output:

The string "pisci" found at offset 43

같이 보기 (See also)

search 요소 범위에서 첫 번째 발생을 검색해요 (함수 템플릿 & 알고리즘 함수 객체) [편집]
ranges::search (C++20)
default_searcher (C++17) 표준 C++ 라이브러리 검색 알고리즘 구현 (클래스 템플릿) [편집]
boyer_moore_horspool_searcher (C++17) Boyer-Moore-Horspool 검색 알고리즘 구현 (클래스 템플릿) [편집]

더 알아보기 (Learn more)

cppreference