std::search
| 헤더 <algorithm>
|
||
template< class ForwardIt1, class ForwardIt2 >
ForwardIt1 search( ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last );
|
에 정의됨 (1) | (C++20부터 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt1 search( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last );
|
(2) | (C++17부터) |
template< class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 search( ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last,
BinaryPred p );
|
(3) | (C++20부터 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 search( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last,
BinaryPred p );
|
(4) | (C++17부터) |
template< class ForwardIt, class Searcher >
ForwardIt search( ForwardIt first, ForwardIt last,
const Searcher& searcher );
|
(5) | (C++17부터) (C++20부터 constexpr) |
[s_first, s_last)의 첫 번째 출현을 범위 [first, last)에서 검색합니다.operator==을(를) 사용하여 비교됩니다.p을(를) 사용하여 비교됩니다.policy에 따라 실행됩니다.true인 경우에만 오버로드 해결에 참여합니다:
|
|
(C++20까지) |
|
|
(C++20부터) |
[first, last)에서 searcher의 생성자에 지정된 패턴을 검색합니다.|
표준 라이브러리는 다음 검색기를 제공합니다:
|
(C++17부터) |
매개변수
| first, last | - | 검사할 요소들의 범위 를 정의하는 반복자 쌍 |
| s_first, s_last | - | 검색할 요소들의 범위 를 정의하는 반복자 쌍 |
| policy | - | 사용할 실행 정책 |
| searcher | - | 검색 알고리즘과 찾을 패턴을 캡슐화하는 검색기 |
| p | - |
요소들이 동등하게 처리되어야 할 경우
true
를 반환하는 이항 predicate.
predicate 함수의 시그니처는 다음과 동등해야 함: bool pred ( const Type1 & a, const Type2 & b ) ;
시그니처가
const
&
를 가질 필요는 없지만, 함수는 전달된 객체를 수정해서는 안 되며
값 범주
에 관계없이 (가능한 const)
|
| 타입 요구사항 | ||
-
ForwardIt1, ForwardIt2
는
LegacyForwardIterator
요구사항을 충족해야 함.
|
||
-
BinaryPred
는
BinaryPredicate
요구사항을 충족해야 함.
|
||
반환값
[
first
,
last
)
에서 시퀀스
[
s_first
,
s_last
)
의 첫 번째 발생 시작 부분에 대한 반복자. 해당 발생이 발견되지 않으면
last
가 반환됩니다.
[
s_first
,
s_last
)
구간이 비어 있으면,
first
가 반환됩니다.
복잡도
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패할 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
| search (1) |
|---|
template<class ForwardIt1, class ForwardIt2> constexpr //< since C++20 ForwardIt1 search(ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last) { while (true) { ForwardIt1 it = first; for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) { if (s_it == s_last) return first; if (it == last) return last; if (!(*it == *s_it)) break; } ++first; } } |
| search (3) |
template<class ForwardIt1, class ForwardIt2, class BinaryPred> constexpr //< since C++20 ForwardIt1 search(ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPred p) { while (true) { ForwardIt1 it = first; for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) { if (s_it == s_last) return first; if (it == last) return last; if (!p(*it, *s_it)) break; } ++first; } } |
예제
#include <algorithm> #include <cassert> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <string_view> #include <vector> using namespace std::literals; bool contains(const auto& cont, std::string_view s) { // str.find() (or str.contains(), since C++23) can be used as well return std::search(cont.begin(), cont.end(), s.begin(), s.end()) != cont.end(); } int main() { const auto str{"why waste time learning, when ignorance is instantaneous?"sv}; assert(contains(str, "learning")); assert(not contains(str, "lemming")); const std::vector vec(str.begin(), str.end()); assert(contains(vec, "learning")); assert(not contains(vec, "leaning")); // The C++17 overload with searchers demo: constexpr auto quote { "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed " "do eiusmod tempor incididunt ut labore et dolore magna aliqua"sv }; for (const auto word : {"pisci"sv, "Pisci"sv}) { std::cout << "The string " << std::quoted(word) << ' '; const std::boyer_moore_searcher searcher(word.begin(), word.end()); const auto it = std::search(quote.begin(), quote.end(), searcher); if (it == quote.end()) std::cout << "not found\n"; else std::cout << "found at offset " << std::distance(quote.begin(), it) << '\n'; } }
출력:
The string "pisci" found at offset 43 The string "Pisci" not found
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 1205 | C++98 |
[
s_first
,
s_last
)
가 비어 있을 때 반환 값이 불명확했음
|
이 경우 first 를 반환함 |
| LWG 1338 | C++98 |
LWG 이슈 1205
의 해결책이 잘못 적용되어,
발견되지 않을 경우 first 를 반환하게 됨 |
이 경우 last 를 반환함 |
| LWG 2150 | C++98 | "시퀀스 발생" 조건이 올바르지 않았음 | 수정됨 |
참고
| 특정 범위에서 마지막으로 나타나는 요소 시퀀스를 찾습니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
| 한 시퀀스가 다른 시퀀스의 부분 시퀀스인지 확인합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
| 두 요소 집합이 동일한지 확인합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
(C++11) |
특정 기준을 충족하는 첫 번째 요소를 찾습니다 (함수 템플릿 & 알고리즘 함수 객체) |
(C++20)(C++20)(C++20) |
|
| 두 범위를 사전식으로 비교합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
| 두 범위가 다른 첫 번째 위치를 찾습니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
| 범위에서 요소의 연속 복사본이 처음 나타나는 위치를 검색합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
(C++17) |
표준 C++ 라이브러리 검색 알고리즘 구현 (클래스 템플릿) |
(C++17) |
Boyer-Moore 검색 알고리즘 구현 (클래스 템플릿) |
| Boyer-Moore-Horspool 검색 알고리즘 구현 (클래스 템플릿) | |
(C++20) |
요소 범위가 처음 나타나는 위치를 검색합니다 (알고리즘 함수 객체) |