Przejdź do głównej zawartości

std::mismatch() algorithm

// (1)
template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2 );

// (2)
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPredicate p );

// (3)
template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 );

// (4)
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPredicate p );

// (5)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2 );

// (6)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class BinaryPredicate >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, BinaryPredicate p );

// (7)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );

// (8)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class BinaryPredicate >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2, BinaryPredicate p )

Returns the first mismatching pair of elements from two ranges:

  • One defined by [first1; last1).
  • Another defined by [first2; last2).

For (1, 2, 5, 6), if last2 is not provided, it denotes first2 + (last1 - first1).

  • (1, 3) Elements are compared using operator==.

  • (2, 4) Elements are compared using the given binary predicate p.

  • (5 - 8) Same as (1 - 4), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.  (od C++20)

Parameters

first1
last1

The first range of elements.

first2
last2

The second range of elements.

policy

The execution policy to use. See execution policy for details.

p

Binary predicate which returns true if the elements should be treated as equal.

The signature of the function should be equivalent to the following:

bool fun(const Type1& a, const Type2& b);
  • The signature does not need to have const&.
  • The function must not modify the objects passed to it
  • Must accept all values of type (possibly const) Type and Type2, regardless of value category (so Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (od C++11))
  • The types Type1 and Type2 must be such that an object of type InputIt1 and InputIt2 can be dereferenced and then implicitly converted to them.

Type requirements

InputIt1
InputIt2
LegacyInputIterator
ForwardIt1
ForwardIt2
LegacyForwardIterator
BinaryPredicateBinaryPredicate

Return value

std::pair with iterators to the first two non-equal elements.

If no mismatches are found when the comparison reaches last1 or last2, whichever happens first, the pair holds the end iterator and the corresponding iterator from the other range.

Complexity

  • (1, 2, 5, 6) At most last1 - first1 applications of operator== or the predicate p.
  • (3, 4, 7, 8) Exactly std::distance(first1, last1) applications of unary_op.

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

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 (2)
template<class InputIt1, class InputIt2, class BinaryPredicate>
std::pair<InputIt1, InputIt2>
mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p)
{
while (first1 != last1 && p(*first1, *first2))
++first1, ++first2;

return std::make_pair(first1, first2);
}
mismatch (3)
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 (4)
template<class InputIt1, class InputIt2, class BinaryPredicate>
std::pair<InputIt1, InputIt2>
mismatch(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPredicate p)
{
while (first1 != last1 && first2 != last2 && p(*first1, *first2))
++first1, ++first2;

return std::make_pair(first1, first2);
}

Examples

This program determines the longest substring that is simultaneously found at the very beginning of the given string and at the very end of it, in reverse order (possibly overlapping).

The following code uses mismatch to convert a string in place to uppercase using the std::toupper function and then mismatchs each char to its ordinal value:

Main.cpp
#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';
}
Output
ab
a
aba
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::mismatch() algorithm

// (1)
template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2 );

// (2)
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPredicate p );

// (3)
template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 );

// (4)
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPredicate p );

// (5)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2 );

// (6)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class BinaryPredicate >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, BinaryPredicate p );

// (7)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );

// (8)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class BinaryPredicate >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2, BinaryPredicate p )

Returns the first mismatching pair of elements from two ranges:

  • One defined by [first1; last1).
  • Another defined by [first2; last2).

For (1, 2, 5, 6), if last2 is not provided, it denotes first2 + (last1 - first1).

  • (1, 3) Elements are compared using operator==.

  • (2, 4) Elements are compared using the given binary predicate p.

  • (5 - 8) Same as (1 - 4), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.  (od C++20)

Parameters

first1
last1

The first range of elements.

first2
last2

The second range of elements.

policy

The execution policy to use. See execution policy for details.

p

Binary predicate which returns true if the elements should be treated as equal.

The signature of the function should be equivalent to the following:

bool fun(const Type1& a, const Type2& b);
  • The signature does not need to have const&.
  • The function must not modify the objects passed to it
  • Must accept all values of type (possibly const) Type and Type2, regardless of value category (so Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (od C++11))
  • The types Type1 and Type2 must be such that an object of type InputIt1 and InputIt2 can be dereferenced and then implicitly converted to them.

Type requirements

InputIt1
InputIt2
LegacyInputIterator
ForwardIt1
ForwardIt2
LegacyForwardIterator
BinaryPredicateBinaryPredicate

Return value

std::pair with iterators to the first two non-equal elements.

If no mismatches are found when the comparison reaches last1 or last2, whichever happens first, the pair holds the end iterator and the corresponding iterator from the other range.

Complexity

  • (1, 2, 5, 6) At most last1 - first1 applications of operator== or the predicate p.
  • (3, 4, 7, 8) Exactly std::distance(first1, last1) applications of unary_op.

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

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 (2)
template<class InputIt1, class InputIt2, class BinaryPredicate>
std::pair<InputIt1, InputIt2>
mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p)
{
while (first1 != last1 && p(*first1, *first2))
++first1, ++first2;

return std::make_pair(first1, first2);
}
mismatch (3)
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 (4)
template<class InputIt1, class InputIt2, class BinaryPredicate>
std::pair<InputIt1, InputIt2>
mismatch(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPredicate p)
{
while (first1 != last1 && first2 != last2 && p(*first1, *first2))
++first1, ++first2;

return std::make_pair(first1, first2);
}

Examples

This program determines the longest substring that is simultaneously found at the very beginning of the given string and at the very end of it, in reverse order (possibly overlapping).

The following code uses mismatch to convert a string in place to uppercase using the std::toupper function and then mismatchs each char to its ordinal value:

Main.cpp
#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';
}
Output
ab
a
aba
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.