std:: adjacent_find
|
헤더에 정의됨
<algorithm>
|
||
|
template
<
class
ForwardIt
>
ForwardIt adjacent_find ( ForwardIt first, ForwardIt last ) ; |
(1) | (constexpr since C++20) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt
>
ForwardIt adjacent_find
(
ExecutionPolicy
&&
policy,
|
(2) | (since C++17) |
|
template
<
class
ForwardIt,
class
BinaryPred
>
ForwardIt adjacent_find
(
ForwardIt first, ForwardIt last,
|
(3) | (constexpr since C++20) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
BinaryPred
>
ForwardIt adjacent_find
(
ExecutionPolicy
&&
policy,
|
(4) | (since C++17) |
범위
[
first
,
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 | - | 검사할 요소들의 범위 를 정의하는 반복자 쌍 |
| policy | - | 사용할 실행 정책 |
| p | - |
요소들이 동등하게 처리되어야 할 경우
true
를 반환하는 이항 predicate
predicate 함수의 시그니처는 다음에 해당해야 합니다: bool pred ( const Type1 & a, const Type2 & b ) ;
시그니처에
const
&
가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며
값 범주
에 관계없이 (가능한 const)
|
| 타입 요구사항 | ||
-
ForwardIt
는
LegacyForwardIterator
의 요구사항을 충족해야 합니다.
|
||
-
BinaryPred
는
BinaryPredicate
의 요구사항을 충족해야 합니다.
|
||
반환값
첫 번째로 동일한 요소 쌍 중 첫 번째 요소에 대한 반복자, 즉 it 와 같이 * it == * ( it + 1 ) 조건을 만족하는 첫 번째 반복자 (1,2) 또는 p ( * it, * ( it + 1 ) ) ! = false 조건을 만족하는 첫 번째 반복자 (3,4) 를 반환합니다.
해당하는 요소가 없으면, last 가 반환됩니다.
복잡도
result
를
adjacent_find
의 반환 값으로,
M
을
std::
distance
(
first, result
)
으로,
N
을
std::
distance
(
first, last
)
으로 가정할 때:
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
| adjacent_find (1) |
|---|
template<class ForwardIt> ForwardIt adjacent_find(ForwardIt first, ForwardIt last) { if (first == last) return last; ForwardIt next = first; ++next; for (; next != last; ++next, ++first) if (*first == *next) return first; return last; } |
| adjacent_find (3) |
template<class ForwardIt, class BinaryPred> ForwardIt adjacent_find(ForwardIt first, ForwardIt last, BinaryPred p) { if (first == last) return last; ForwardIt next = first; ++next; for (; next != last; ++next, ++first) if (p(*first, *next)) return first; return last; } |
`, `
` 태그 내 C++ 코드는 번역하지 않음
- "adjacent_find" 같은 C++ 전문 용어는 번역하지 않음
- 표 구조와 서식 완전히 보존
- 링크와 클래스명 등 모든 기술적 요소 변경 없음
예제
#include <algorithm> #include <functional> #include <iostream> #include <vector> int main() { std::vector<int> v1{0, 1, 2, 3, 40, 40, 41, 41, 5}; auto i1 = std::adjacent_find(v1.begin(), v1.end()); if (i1 == v1.end()) std::cout << "No matching adjacent elements\n"; else std::cout << "The first adjacent pair of equal elements is at " << std::distance(v1.begin(), i1) << ", *i1 = " << *i1 << '\n'; auto i2 = std::adjacent_find(v1.begin(), v1.end(), std::greater<int>()); if (i2 == v1.end()) std::cout << "The entire vector is sorted in ascending order\n"; else std::cout << "The last element in the non-decreasing subsequence is at " << std::distance(v1.begin(), i2) << ", *i2 = " << *i2 << '\n'; }
출력:
The first adjacent pair of equal elements is at 4, *i1 = 40 The last element in the non-decreasing subsequence is at 7, *i2 = 41
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 240 | C++98 |
술어가
std::
find
( first, last, value ) - first 번 적용됨 (1,3) 의 경우, value 가 정의된 적 없음 |
std::
min
(
( result - first ) + 1 , ( last - first ) - 1 ) 번 적용됨 |
참고 항목
|
범위에서 연속된 중복 요소를 제거합니다
(함수 템플릿) |
|
|
(C++20)
|
서로 같은(또는 주어진 조건자를 만족하는) 첫 번째 인접한 두 항목을 찾습니다
(알고리즘 함수 객체) |