std::mismatch
| 헤더 <algorithm>
|
||
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2 );
|
에 정의됨 (1) | (C++20부터 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2 );
|
(2) | (C++17부터) |
template< class InputIt1, class InputIt2, class BinaryPred >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPred p );
|
(3) | (C++20부터 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class BinaryPred >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, BinaryPred p );
|
(4) | (C++17부터) |
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 );
|
(5) | (C++14부터) (C++20부터 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );
|
(6) | (C++17부터) |
template< class InputIt1, class InputIt2, class BinaryPred >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPred p );
|
(7) | (C++14부터) (C++20부터 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class BinaryPred >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2, BinaryPred p );
|
(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에 도달하면 비교가 중지됩니다.
operator==을(를) 사용하여 비교됩니다.p을(를) 사용하여 비교됩니다.policy에 따라 실행됩니다.true일 때만 오버로드 해결에 참여합니다:
|
|
(C++20까지) |
|
|
(C++20부터) |
매개변수
| first1, last1 | - | 비교할 첫 번째 범위를 정의하는 반복자 쌍 |
| first2, last2 | - | 비교할 두 번째 범위를 정의하는 반복자 쌍 |
| policy | - | 사용할 실행 정책 |
| p | - | 요소가 같다고 처리되어야 하는 경우 true을(를) 반환하는 이진 술어. 술어 함수의 시그니처는 다음과 같아야 합니다:
시그니처에 |
| 유형 요구 사항 | ||
-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 다음 반복자입니다.
복잡도
N1을 std::distance(first1, last1)로, N2를 std::distance(first2, last2)로 할 때:
operator== 비교.p 적용.operator== 비교.p 적용.예외
ExecutionPolicy이라는 템플릿 매개변수가 있는 오버로드는 다음과 같이 오류를 보고합니다:
- 알고리즘의 일부로 호출된 함수가 예외를 던지고
ExecutionPolicy이(가) 표준 정책 중 하나인 경우, std::terminate가 호출됩니다. 다른ExecutionPolicy의 경우, 동작은 구현 정의입니다. - 알고리즘이 메모리 할당에 실패하면, std::bad_alloc이 던져집니다.
가능한 구현
| 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++20) |
|
(C++11) |
특정 기준을 충족하는 첫 번째 요소를 찾습니다 (함수 템플릿 & 알고리즘 함수 객체) |
(C++20)(C++20)(C++20) |
|
| 두 범위를 사전식으로 비교합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
| 요소 범위의 첫 번째 출현을 검색합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
(C++20) |
두 범위가 다른 첫 번째 위치를 찾습니다 (알고리즘 함수 객체) |