Namespaces
Variants

std:: mismatch

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11) (C++11) (C++11)
mismatch

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
헤더에 정의됨 <algorithm>
template < class InputIt1, class InputIt2 >

std:: pair < InputIt1, InputIt2 >
mismatch ( InputIt1 first1, InputIt1 last1,

InputIt2 first2 ) ;
(1) (constexpr since C++20)
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 에 따라 실행됩니다.
다음 모든 조건이 만족될 때만 이 오버로드들이 오버로드 해결에 참여합니다:

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인) Type1 Type2 타입의 모든 값을 수용할 수 있어야 함 (따라서 Type1 & 는 허용되지 않으며, Type1 또한 Type1 에 대해 이동이 복사와 동등하지 않는 한 허용되지 않음 (C++11부터) ).
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 이후의 반복자입니다.

복잡도

N 1 std:: distance ( first1, last1 ) 로, N 2 std:: distance ( first2, last2 ) 로 정의할 때:

1,2) 최대 N 1 번의 비교를 operator == 를 사용하여 수행합니다.
3,4) 최대 N 1 번의 술어 p 적용.
5,6) 최대 min(N 1 ,N 2 ) 번의 비교를 operator == 를 사용하여 수행합니다.
7,8) 최대 min(N 1 ,N 2 ) 번의 predicate p 적용.

예외

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);
}
**번역 결과:** - 모든 HTML 태그와 속성은 원본 그대로 유지됨 - ` `, `
`, `` 태그 내부의 C++ 코드는 번역하지 않음
- C++ 관련 용어(mismatch, template, pair 등)는 번역하지 않음
- 표 구조와 서식은 완전히 보존됨
- 링크와 코드 하이라이팅도 원본과 동일하게 유지됨

예제

이 프로그램은 주어진 문자열의 가장 앞쪽과 가장 뒤쪽에서 역순으로 동시에 발견되는 가장 긴 부분 문자열을 결정합니다(중첩될 수 있음).

#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

참고 항목

두 원소 집합이 동일한지 판별합니다
(함수 템플릿)
특정 조건을 만족하는 첫 번째 원소를 찾습니다
(함수 템플릿)
한 범위가 다른 범위보다 사전순으로 작으면 true 를 반환합니다
(함수 템플릿)
원소 범위의 첫 번째 발생을 검색합니다
(함수 템플릿)
두 범위가 처음으로 달라지는 위치를 찾습니다
(알고리즘 함수 객체)