std:: find_first_of
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
InputIt,
class
ForwardIt
>
InputIt find_first_of
(
InputIt first, InputIt last,
|
(1) | (constexpr since C++20) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2
>
ForwardIt1 find_first_of
(
ExecutionPolicy
&&
policy,
|
(2) | (since C++17) |
|
template
<
class
InputIt,
class
ForwardIt,
class
BinaryPred
>
InputIt find_first_of
(
InputIt first, InputIt last,
|
(3) | (constexpr since C++20) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
class
BinaryPred
>
|
(4) | (since 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
&
를 가질 필요는 없지만, 함수는 전달된 객체를 수정해서는 안 되며
값 범주
에 관계없이
|
| 타입 요구사항 | ||
-
InputIt
는
LegacyInputIterator
의 요구사항을 충족해야 함.
|
||
-
ForwardIt
는
LegacyForwardIterator
의 요구사항을 충족해야 함.
|
||
-
ForwardIt1
는
LegacyForwardIterator
의 요구사항을 충족해야 함.
|
||
-
ForwardIt2
는
LegacyForwardIterator
의 요구사항을 충족해야 함.
|
||
-
BinaryPred
는
BinaryPredicate
의 요구사항을 충족해야 함.
|
||
반환값
범위
[
first
,
last
)
내에서 범위
[
s_first
,
s_last
)
의 요소와 동일한 첫 번째 요소에 대한 iterator.
만약
[
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_first_of (1) |
|---|
template<class InputIt, class ForwardIt> InputIt find_first_of(InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last) { for (; first != last; ++first) for (ForwardIt it = s_first; it != s_last; ++it) if (*first == *it) return first; return last; } |
| find_first_of (3) |
template<class InputIt, class ForwardIt, class BinaryPred> InputIt find_first_of(InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last, BinaryPred p) { for (; first != last; ++first) for (ForwardIt it = s_first; it != s_last; ++it) if (p(*first, *it)) return first; return last; } |
예제
다음 코드는 정수 벡터에서 지정된 정수들 중 하나를 검색합니다:
#include <algorithm> #include <iostream> #include <vector> auto print_sequence = [](const auto id, const auto& seq, int pos = -1) { std::cout << id << "{ "; for (int i{}; auto const& e : seq) { const bool mark{i == pos}; std::cout << (i++ ? ", " : ""); std::cout << (mark ? "[ " : "") << e << (mark ? " ]" : ""); } std::cout << " }\n"; }; int main() { const std::vector<int> v{0, 2, 3, 25, 5}; const auto t1 = {19, 10, 3, 4}; const auto t2 = {1, 6, 7, 9}; auto find_any_of = [](const auto& v, const auto& t) { const auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end()); if (result == v.end()) { std::cout << "No elements of v are equal to any element of "; print_sequence("t = ", t); print_sequence("v = ", v); } else { const auto pos = std::distance(v.begin(), result); std::cout << "Found a match (" << *result << ") at position " << pos; print_sequence(", where t = ", t); print_sequence("v = ", v, pos); } }; find_any_of(v, t1); find_any_of(v, t2); }
출력:
Found a match (3) at position 2, where t = { 19, 10, 3, 4 }
v = { 0, 2, [ 3 ], 25, 5 }
No elements of v are equal to any element of t = { 1, 6, 7, 9 }
v = { 0, 2, 3, 25, 5 }
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 576 | C++98 | first 와 last 가 LegacyForwardIterator s 여야 했음 |
이들은
LegacyInputIterator s 이기만 하면 됨 |
| LWG 1205 | C++98 |
[
s_first
,
s_last
)
가 비어 있을 때 반환 값이 불명확했음
|
이 경우 last 를 반환함 |
참고 항목
|
(C++11)
|
특정 조건을 만족하는 첫 번째 요소를 찾음
(함수 템플릿) |
|
(C++20)
|
요소 집합 중 하나를 검색함
(알고리즘 함수 객체) |