Namespaces
Variants

std::mismatch

cppreference.net에서 가져옴
 
 
알고리즘 라이브러리
제약된 알고리즘 및 범위 기반 알고리즘 (C++20)
제약된 알고리즘 (예: ranges::copy, ranges::sort, ...)
정렬 및 관련 연산
분할 연산
(C++11)    

정렬 연산
이진 검색 연산
(분할된 범위에 대해)
집합 연산 (정렬된 범위에 대해)
병합 연산 (정렬된 범위에 대해)
힙 연산
최소/최대 연산
(C++11)
(C++17)
사전식 비교 연산
순열 연산


 
헤더 <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에 도달하면 비교가 중지됩니다.
1,5) 요소는 operator==을(를) 사용하여 비교됩니다.
3,7) 요소는 주어진 이진 술어 p을(를) 사용하여 비교됩니다.
2,4,6,8) (1,3,5,7)과 동일하지만, policy에 따라 실행됩니다.
이 오버로드는 다음 표현식의 값이 true일 때만 오버로드 해결에 참여합니다:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(C++20까지)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(C++20부터)

매개변수

first1, last1 - 비교할 첫 번째 범위를 정의하는 반복자 쌍
first2, last2 - 비교할 두 번째 범위를 정의하는 반복자 쌍
policy - 사용할 실행 정책
p - 요소가 같다고 처리되어야 하는 경우 ​true을(를) 반환하는 이진 술어.

술어 함수의 시그니처는 다음과 같아야 합니다:

bool pred(const Type1 &a, const Type2 &b);

시그니처에 const &이(가) 있을 필요는 없지만, 함수는 전달된 객체를 수정하지 않아야 하며, 값 범주Type1 (따라서 Type2은(는) 허용되지 않음, 은(는) Type1 & 이동이 복사와 동등한 경우가 아니면 허용되지 않음(C++11부터)Type1)에 관계없이 Type1 유형( const일 수 있음)의 모든 값을 받아들일 수 있어야 합니다.
유형 Type1과 Type2는 InputIt1 및 InputIt2 유형의 객체를 역참조한 후 각각 Type1 및 Type2으로 암시적으로 변환할 수 있어야 합니다. ​

유형 요구 사항
-
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)로 할 때:

1,2) 최대 N1회의 operator== 비교.
3,4) 최대 N1회의 술어 p 적용.
5,6) 최대 min(N1,N2)회의 operator== 비교.
7,8) 최대 min(N1,N2)회의 술어 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

참고 항목

두 요소 집합이 같은지 결정합니다
(함수 템플릿 & 알고리즘 함수 객체)
특정 기준을 충족하는 첫 번째 요소를 찾습니다
(함수 템플릿 & 알고리즘 함수 객체)
두 범위를 사전식으로 비교합니다
(함수 템플릿 & 알고리즘 함수 객체)
요소 범위의 첫 번째 출현을 검색합니다
(함수 템플릿 & 알고리즘 함수 객체)
두 범위가 다른 첫 번째 위치를 찾습니다
(알고리즘 함수 객체)