std:: mismatch
|
헤더에 정의됨
<algorithm>
|
||
|
template
<
class
InputIt1,
class
InputIt2
>
std::
pair
<
InputIt1, InputIt2
>
|
(1) | (constexpr since C++20) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2
>
std::
pair
<
ForwardIt1, ForwardIt2
>
|
(2) | (C++17부터) |
|
template
<
class
InputIt1,
class
InputIt2,
class
BinaryPred
>
std::
pair
<
InputIt1, InputIt2
>
|
(3) | (C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
class
BinaryPred
>
|
(4) | (C++17 이후) |
|
template
<
class
InputIt1,
class
InputIt2
>
std::
pair
<
InputIt1, InputIt2
>
|
(5) |
(C++14부터)
(C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2
>
std::
pair
<
ForwardIt1, ForwardIt2
>
|
(6) | (C++17 이후) |
|
template
<
class
InputIt1,
class
InputIt2,
class
BinaryPred
>
std::
pair
<
InputIt1, InputIt2
>
|
(7) |
(C++14부터)
(C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
class
BinaryPred
>
|
(8) | (C++17 이후) |
[
first1
,
last1
)
범위와
first2
에서 시작하는 범위 간의 첫 번째 불일치 요소에 대한 반복자 쌍을 반환합니다:
- 오버로드 (1-4) 의 경우, 두 번째 범위는 std:: distance ( first1, last1 ) 개의 요소를 가집니다.
-
오버로드
(5-8)
의 경우, 두 번째 범위는
[first2,last2)입니다.
-
- 만약 std:: distance ( first1, last1 ) 와 std:: distance ( first2, last2 ) 가 다르면, 비교는 last1 또는 last2 에 도달했을 때 중단됩니다.
|
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 | - | 사용할 실행 정책 |
| p | - |
원소들이 동등하게 처리되어야 할 경우
true
를 반환하는 이항 predicate.
predicate 함수의 시그니처는 다음에 부합해야 함: bool pred ( const Type1 & a, const Type2 & b ) ;
시그니처에
const
&
가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며,
값 범주
에 관계없이 (아마도 const인)
|
| 타입 요구 사항 | ||
-
InputIt1
는
LegacyInputIterator
요구 사항을 충족해야 함.
|
||
-
InputIt2
는
LegacyInputIterator
요구 사항을 충족해야 함.
|
||
-
ForwardIt1
는
LegacyForwardIterator
요구 사항을 충족해야 함.
|
||
-
ForwardIt2
는
LegacyForwardIterator
요구 사항을 충족해야 함.
|
||
-
BinaryPred
는
BinaryPredicate
요구 사항을 충족해야 함.
|
||
반환값
std::pair 첫 번째 두 개의 서로 다른 요소를 가리키는 반복자와 함께.
만약 last1 에 도달하면, 쌍의 두 번째 반복자는 std:: distance ( first1, last1 ) 번째 first2 이후의 반복자입니다.
오버로드 (5-8) 의 경우, last2 에 도달하면, 쌍의 첫 번째 반복자는 std:: distance ( first2, last2 ) 번째 first1 이후의 반복자입니다.
복잡도
N 1 를 std:: distance ( first1, last1 ) 로, N 2 를 std:: distance ( first2, last2 ) 로 정의할 때:
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패할 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
| mismatch (1) |
|---|
template<class InputIt1, class InputIt2> std::pair<InputIt1, InputIt2> mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2) { while (first1 != last1 && *first1 == *first2) ++first1, ++first2; return std::make_pair(first1, first2); } |
| mismatch (3) |
template<class InputIt1, class InputIt2, class BinaryPred> std::pair<InputIt1, InputIt2> mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPred p) { while (first1 != last1 && p(*first1, *first2)) ++first1, ++first2; return std::make_pair(first1, first2); } |
| mismatch (5) |
template<class InputIt1, class InputIt2> std::pair<InputIt1, InputIt2> mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) { while (first1 != last1 && first2 != last2 && *first1 == *first2) ++first1, ++first2; return std::make_pair(first1, first2); } |
| mismatch (7) |
template<class InputIt1, class InputIt2, class BinaryPred> std::pair<InputIt1, InputIt2> mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPred p) { while (first1 != last1 && first2 != last2 && p(*first1, *first2)) ++first1, ++first2; return std::make_pair(first1, first2); } |
`, `
`, `
예제
이 프로그램은 주어진 문자열의 가장 앞쪽과 가장 뒤쪽에서 역순으로 동시에 발견되는 가장 긴 부분 문자열을 결정합니다(중첩될 수 있음).
#include <algorithm> #include <iostream> #include <string> std::string mirror_ends(const std::string& in) { return std::string(in.begin(), std::mismatch(in.begin(), in.end(), in.rbegin()).first); } int main() { std::cout << mirror_ends("abXYZba") << '\n' << mirror_ends("abca") << '\n' << mirror_ends("aba") << '\n'; }
출력:
ab a aba
참고 항목
|
두 원소 집합이 동일한지 판별합니다
(함수 템플릿) |
|
|
(C++11)
|
특정 조건을 만족하는 첫 번째 원소를 찾습니다
(함수 템플릿) |
|
한 범위가 다른 범위보다 사전순으로 작으면
true
를 반환합니다
(함수 템플릿) |
|
|
원소 범위의 첫 번째 발생을 검색합니다
(함수 템플릿) |
|
|
(C++20)
|
두 범위가 처음으로 달라지는 위치를 찾습니다
(알고리즘 함수 객체) |