functional_default_searcher

functional_default_searcher (기본 검색기 클래스 템플릿)

std::default_searcher는 C++17부터 <functional> 헤더에 정의된 클래스 템플릿이에요. 이 타입은 std::search의 Searcher 오버로드와 함께 사용되어 검색 작업을 C++17 이전 표준 라이브러리의 std::search에 위임해요. std::default_searcher는 복사 생성 가능(CopyConstructible)하고 복사 할당 가능(CopyAssignable)한 타입이에요.

출처: cppreference

본문

정의

Defined in header <functional>
template < class ForwardIt , class BinaryPredicate = std :: equal_to <> > class default_searcher ; (since C++17)

std::default_searcherstd::search의 Searcher 오버로드와 함께 사용하기에 적합한 클래스예요. 검색 작업을 C++17 이전 표준 라이브러리의 std::search에 위임해요.

std::default_searcher는 복사 생성 가능(CopyConstructible)하고 복사 할당 가능(CopyAssignable)해요.

멤버 함수

std::default_searcher::default_searcher

default_searcher ( ForwardIt pat_first , ForwardIt pat_last , BinaryPredicate pred = BinaryPredicate () ); (since C++17) (constexpr since C++20)

pat_first, pat_last, pred의 복사본을 저장하여 std::default_searcher를 생성해요.

매개변수
pat_first, pat_last - 검색 대상 문자열을 지정하는 반복자 쌍
pred - 동일성을 판별하는 데 사용되는 호출 가능한 객체
예외

BinaryPredicate 또는 ForwardIt의 복사 생성자가 던지는 모든 예외가 전달돼요.

std::default_searcher::operator()

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

이 검색기로 검색을 수행하기 위해 std::search의 Searcher 오버로드가 호출하는 멤버 함수예요.

반복자 쌍 i, j를 반환해요. 여기서 istd::search(first, last, pat_first, pat_last, pred)이고, jstd::next(i, std::distance(pat_first, pat_last))예요. 단, std::searchlast를 반환한 경우(일치 항목 없음)에는 jlast와 같아요.

매개변수
first, last - 검사 대상 문자열을 지정하는 반복자 쌍
반환값

[first, last) 범위에서 pred에 따라 [pat_first, pat_last)와 동일하게 비교되는 부분 수열이 위치한 첫 번째 위치와 그 마지막 다음 위치를 가리키는 반복자 쌍을 반환해요. 일치하는 항목이 없으면 last의 복사본 쌍을 반환해요.

예제

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string_view>
 
int main()
{
    constexpr std::string_view in =
        "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"};

    auto it = std::search(in.begin(), in.end(),
                  std::default_searcher(
                      needle.begin(), needle.end()));
    if (it != in.end())
        std::cout << "The string " << std::quoted(needle) << " found at offset "
                  << it - in.begin() << '\n';
    else
        std::cout << "The string " << std::quoted(needle) << " not found\n";
}

출력:

The string "pisci" found at offset 43

같이 보기

search 요소 범위에서 첫 번째로 나타나는 위치를 검색해요 (함수 템플릿 & 알고리즘 함수 객체)
ranges::search (C++20)
boyer_moore_searcher (C++17) Boyer-Moore 검색 알고리즘 구현 (클래스 템플릿)
boyer_moore_horspool_searcher (C++17) Boyer-Moore-Horspool 검색 알고리즘 구현 (클래스 템플릿)

더 알아보기 (Learn more)

cppreference