std::find_end
| 헤더 <algorithm>
|
||
template< class ForwardIt1, class ForwardIt2 >
ForwardIt1 find_end( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );
|
에 정의됨 (1) | (C++20부터 constexpr) |
template< class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_end( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
BinaryPred p );
|
(2) | (C++20부터 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt1 find_end( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );
|
(3) | (C++17부터) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_end( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
BinaryPred p );
|
(4) | (C++17부터) |
소스 범위 [first2, last2) 에서 대상 범위 [first1, last1) 의 마지막 출현을 검색합니다.
operator== 을 사용하여 비교됩니다.p 을 사용하여 비교됩니다.policy 에 따라 실행됩니다.true 인 경우에만 오버로드 해결에 참여합니다:
|
|
(C++20까지) |
|
|
(C++20부터) |
매개변수
| first1, last1 | - | 소스 범위 |
| 를 정의하는 반복자 쌍 | first2, last2 | - 대상 |
| 범위 | 를 정의하는 반복자 쌍 | p
true-
요소가 동일한 것으로 간주되어야 하면
를 반환하는 이진 술어. |
| 로 변환될 수 있어야 합니다. | policy | - 사용할 실행 정책 |
타입 요구사항
ForwardIt1- 는 LegacyForwardIterator | ||
의 요구사항을 충족해야 합니다.
ForwardIt2- 는 LegacyForwardIterator | ||
의 요구사항을 충족해야 합니다.
반환값
소스 범위에서 대상 범위의 마지막 출현의 시작을 가리키는 반복자.
last1대상 범위가 비어 있거나 소스 범위에 나타나지 않으면
가 반환됩니다.
복잡도N1 을 std::distance(first1, last1) 로 하고 N2 을 std::distance(first2, last2) 로 할 때:
operator== 비교.p 적용.operator== 비교.p 적용.예외
- 병렬화에 필요한 임시 메모리 자원을 사용할 수 없는 경우 std::bad_alloc 이 발생합니다.
- 알고리즘 인수를 통해 객체에 접근하는 중에 잡히지 않은 예외가 발생하면 실행 정책에 따라 동작이 결정됩니다 (표준 정책의 경우 std::terminate 가 호출됩니다).
가능한 구현
| find_end (1) |
|---|
template<class ForwardIt1, class ForwardIt2>
constexpr //< since C++20
ForwardIt1 find_end(ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2)
{
if (first2 == last2)
return last1;
ForwardIt1 result = last1;
while (true)
{
ForwardIt1 new_result = std::search(first1, last1, first2, last2);
if (new_result == last1)
break;
else
{
result = new_result;
first1 = result;
++first1;
}
}
return result;
}
|
| find_end (2) |
template<class ForwardIt1, class ForwardIt2, class BinaryPred>
constexpr //< since C++20
ForwardIt1 find_end(ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2, BinaryPred p)
{
if (first2== last2)
return last1;
ForwardIt1 result = last1;
while (true)
{
ForwardIt1 new_result = std::search(first1, last1, first2, first2, p);
if (new_result == last1)
break;
else
{
result = new_result;
first1 = result;
++first1;
}
}
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 (const auto& 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 (const auto& 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 | 대상 범위가 비어 있을 때 반환값이 명확하지 않았음 | 이 경우 last1 반환
|
| LWG 2150 | C++98 | "출현" 조건이 올바르지 않았음 | 수정됨 |
참조
(C++20) |
특정 범위에서 마지막 요소 시퀀스를 찾습니다 (알고리즘 함수 객체) |
| 요소 범위의 첫 번째 출현을 검색합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
| 하나의 시퀀스가 다른 시퀀스의 부분 시퀀스인지 판별합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
| 동일하거나 주어진 술어를 충족하는 처음 두 개의 인접한 항목을 찾습니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
(C++11) |
특정 기준을 충족하는 첫 번째 요소를 찾습니다 (함수 템플릿 & 알고리즘 함수 객체) |
(C++20)(C++20)(C++20) |
|
| 요소 집합 중 하나를 검색합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
| 범위에서 요소의 연속된 복사본의 첫 번째 출현을 검색합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |