std:: includes
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
InputIt1,
class
InputIt2
>
bool
includes
(
InputIt1 first1, InputIt1 last1,
|
(1) | (C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2
>
|
(2) | (C++17부터) |
|
template
<
class
InputIt1,
class
InputIt2,
class
Compare
>
bool
includes
(
InputIt1 first1, InputIt1 last1,
|
(3) | (C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
class
Compare
>
|
(4) | (C++17부터) |
정렬된 범위
[
first2
,
last2
)
가 정렬된 범위
[
first1
,
last1
)
의
부분 수열
이면
true
를 반환합니다(부분 수열은 연속될 필요가 없습니다).
[
first1
,
last1
)
또는
[
first2
,
last2
)
범위가
정렬되어 있지 않다면
(
operator
<
(C++20 이전)
std::
less
{
}
(C++20 이후)
기준), 동작은 정의되지 않습니다.
[
first1
,
last1
)
또는
[
first2
,
last2
)
가
comp
에 대해 정렬되어 있지 않으면, 동작은 정의되지 않습니다.
|
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 이후) |
목차 |
매개변수
| first1, last1 | - | 검사할 요소들의 정렬된 범위 를 정의하는 반복자 쌍 |
| first2, last2 | - | 검색할 요소들의 정렬된 범위 를 정의하는 반복자 쌍 |
| policy | - | 사용할 실행 정책 |
| comp | - |
비교 함수 객체(즉,
Compare
요구 사항을 만족하는 객체)로, 첫 번째 인수가 두 번째 인수보다
작으면
(즉, 순서상
앞서면
)
true
를 반환함.
비교 함수의 시그니처는 다음과 동일해야 함: bool cmp ( const Type1 & a, const Type2 & b ) ;
시그니처에
const
&
가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며,
값 범주
에 관계없이 (가능한 const)
|
| 타입 요구 사항 | ||
-
InputIt1, InputIt2
는
LegacyInputIterator
요구 사항을 충족해야 함.
|
||
-
ForwardIt1, ForwardIt2
는
LegacyForwardIterator
요구 사항을 충족해야 함.
|
||
-
Compare
는
Compare
요구 사항을 충족해야 함.
|
||
반환값
true
만약
[
first2
,
last2
)
가
[
first1
,
last1
)
의 부분 수열인 경우; 그렇지 않으면
false
.
빈 시퀀스는 모든 시퀀스의 부분 시퀀스이므로,
true
가 반환됩니다 만약
[
first2
,
last2
)
가 빈 경우.
복잡도
N 1 를 std:: distance ( first1, last1 ) 로, N 2 를 std:: distance ( first2, last2 ) 로 정의할 때:
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
| 포함 (1) |
|---|
template<class InputIt1, class InputIt2> bool includes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) { for (; first2 != last2; ++first1) { if (first1 == last1 || *first2 < *first1) return false; if (!(*first1 < *first2)) ++first2; } return true; } |
| 포함 (3) |
template<class InputIt1, class InputIt2, class Compare> bool includes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp) { for (; first2 != last2; ++first1) { if (first1 == last1 || comp(*first2, *first1)) return false; if (!comp(*first1, *first2)) ++first2; } return true; } |
예제
#include <algorithm> #include <cctype> #include <iostream> template<class Os, class Co> Os& operator<<(Os& os, const Co& v) { for (const auto& i : v) os << i << ' '; return os << '\t'; } int main() { const auto v1 = {'a', 'b', 'c', 'f', 'h', 'x'}, v2 = {'a', 'b', 'c'}, v3 = {'a', 'c'}, v4 = {'a', 'a', 'b'}, v5 = {'g'}, v6 = {'a', 'c', 'g'}, v7 = {'A', 'B', 'C'}; auto no_case = [](char a, char b) { return std::tolower(a) < std::tolower(b); }; std::cout << v1 << "\nincludes:\n" << std::boolalpha << v2 << ": " << std::includes(v1.begin(), v1.end(), v2.begin(), v2.end()) << '\n' << v3 << ": " << std::includes(v1.begin(), v1.end(), v3.begin(), v3.end()) << '\n' << v4 << ": " << std::includes(v1.begin(), v1.end(), v4.begin(), v4.end()) << '\n' << v5 << ": " << std::includes(v1.begin(), v1.end(), v5.begin(), v5.end()) << '\n' << v6 << ": " << std::includes(v1.begin(), v1.end(), v6.begin(), v6.end()) << '\n' << v7 << ": " << std::includes(v1.begin(), v1.end(), v7.begin(), v7.end(), no_case) << " (case-insensitive)\n"; }
출력:
a b c f h x includes: a b c : true a c : true a a b : false g : false a c g : false A B C : true (case-insensitive)
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 1205 | C++98 |
[
first2
,
last2
)
가 비어 있을 때 반환 값이 불명확했음
|
이 경우 true 를 반환함 |
참고 항목
|
두 집합의 차집합을 계산함
(함수 템플릿) |
|
|
요소 범위의 첫 번째 발생을 검색함
(함수 템플릿) |
|
|
(C++20)
|
한 시퀀스가 다른 시퀀스의 부분 시퀀스이면
true
를 반환함
(알고리즘 함수 객체) |