std:: find_end
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
ForwardIt1,
class
ForwardIt2
>
ForwardIt1 find_end
(
ForwardIt1 first, ForwardIt1 last,
|
(1) | (C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2
>
ForwardIt1 find_end
(
ExecutionPolicy
&&
policy,
|
(2) | (C++17부터) |
|
template
<
class
ForwardIt1,
class
ForwardIt2,
class
BinaryPred
>
ForwardIt1 find_end
(
ForwardIt1 first, ForwardIt1 last,
|
(3) | (C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
class
BinaryPred
>
|
(4) | (C++17부터) |
범위
[
first
,
last
)
에서 시퀀스
[
s_first
,
s_last
)
의 마지막 발생을 검색합니다.
|
std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> 가 true 입니다. |
(C++20 이전) |
|
std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> 가 true 입니다. |
(C++20 이후) |
목차 |
매개변수
| first, last | - | 검사할 요소들의 범위 를 정의하는 반복자 쌍 |
| s_first, s_last | - | 검색할 요소들의 범위 를 정의하는 반복자 쌍 |
| policy | - | 사용할 실행 정책 |
| p | - |
요소들이 동등하게 처리되어야 할 경우
true
를 반환하는 이항 predicate
predicate 함수의 시그니처는 다음에 해당해야 합니다: bool pred ( const Type1 & a, const Type2 & b ) ;
시그니처에
const
&
가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며,
값 범주
에 관계없이 (가능한 const)
|
| 타입 요구사항 | ||
-
ForwardIt1
는
LegacyForwardIterator
의 요구사항을 충족해야 합니다.
|
||
-
ForwardIt2
는
LegacyForwardIterator
의 요구사항을 충족해야 합니다.
|
||
반환값
범위
[
first
,
last
)
에서 시퀀스
[
s_first
,
s_last
)
의 마지막 발생 시작 부분에 대한 반복자.
만약
[
s_first
,
s_last
)
가 비어 있거나 그러한 시퀀스를 찾을 수 없는 경우,
last
가 반환됩니다.
복잡도
주어진 N 을 std:: distance ( first, last ) 으로, S 를 std:: distance ( s_first, s_last ) 으로 정의합니다:
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
| find_end (1) |
|---|
template<class ForwardIt1, class ForwardIt2> constexpr //< since C++20 ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last) { if (s_first == s_last) return last; ForwardIt1 result = last; while (true) { ForwardIt1 new_result = std::search(first, last, s_first, s_last); if (new_result == last) break; else { result = new_result; first = result; ++first; } } return result; } |
| find_end (3) |
template<class ForwardIt1, class ForwardIt2, class BinaryPred> constexpr //< since C++20 ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPred p) { if (s_first == s_last) return last; ForwardIt1 result = last; while (true) { ForwardIt1 new_result = std::search(first, last, s_first, s_last, p); if (new_result == last) break; else { result = new_result; first = result; ++first; } } return result; } |
예제
#include <algorithm> #include <array> #include <cmath> #include <iostream> auto print_result = [](auto result, const auto& v) { result == v.end() ? std::cout << "Sequence not found\n" : std::cout << "Last occurrence is at: " << std::distance(v.begin(), result) << '\n'; }; int main() { const auto v = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}; for (auto const& x : {std::array{1, 2, 3}, {4, 5, 6}}) { auto iter = std::find_end(v.begin(), v.end(), x.begin(), x.end()); // overload (1) print_result(iter, v); } for (auto const& x : {std::array{-1, -2, -3}, {-4, -5, -6}}) { auto iter = std::find_end(v.begin(), v.end(), x.begin(), x.end(), // overload (3) [](int x, int y) { return std::abs(x) == std::abs(y); }); print_result(iter, v); } }
출력:
Last occurrence is at: 8 Sequence not found Last occurrence is at: 8 Sequence not found
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 1205 | C++98 |
[
s_first
,
s_last
)
구간이 비어있는 경우 반환값이 불명확했음
|
이 경우 last 를 반환함 |
| LWG 2150 | C++98 | "시퀀스 발생" 조건이 부정확했음 | 수정됨 |
참고 항목
|
요소 범위의 첫 번째 발생을 검색합니다
(함수 템플릿) |
|
|
한 시퀀스가 다른 시퀀스의 부분 시퀀스이면
true
를 반환합니다
(함수 템플릿) |
|
|
동일한(또는 주어진 조건자를 만족하는) 첫 번째 두 인접 항목을 찾습니다
(함수 템플릿) |
|
|
(C++11)
|
특정 기준을 만족하는 첫 번째 요소를 찾습니다
(함수 템플릿) |
|
요소 집합 중 하나를 검색합니다
(함수 템플릿) |
|
|
범위에서 요소의 연속된 복사본 개수의 첫 번째 발생을 검색합니다
(함수 템플릿) |
|
|
(C++20)
|
특정 범위에서 요소들의 마지막 시퀀스를 찾습니다
(알고리즘 함수 객체) |