std::mismatch() algorithm
- od C++20
- od C++17
- od C++14
- do C++14
// (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 )
// (1)
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2 );
// (2)
template< class InputIt1, class InputIt2, class BinaryPredicate >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPredicate p );
// (3)
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 );
// (4)
template< class InputIt1, class InputIt2, class BinaryPredicate >
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 )
// (1)
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2 );
// (2)
template< class InputIt1, class InputIt2, class BinaryPredicate >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPredicate p );
// (3)
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 );
// (4)
template< class InputIt1, class InputIt2, class BinaryPredicate >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPredicate p );
// (1)
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2 );
// (2)
template< class InputIt1, class InputIt2, class BinaryPredicate >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, 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 ResolutionThese overloads participate in overload resolution only if
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>
istrue
. (do C++20)std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>
istrue
. (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 The signature of the function should be equivalent to the following:
|
Type requirements
InputIt1 InputIt2 | LegacyInputIterator |
ForwardIt1 ForwardIt2 | LegacyForwardIterator |
BinaryPredicate | BinaryPredicate |
Return value
std::pair
with iterators to the first two non-equal elements.
- od C++14
- do C++14
last1
or last2
, whichever happens first, the pair holds the end iterator and the corresponding iterator from the other range.last1
, the pair holds last1
and the corresponding iterator from the second range.The behavior is undefined if the second range is shorter than the first range.Complexity
- (1, 2, 5, 6) At most
last1 - first1
applications ofoperator==
or the predicatep
. - (3, 4, 7, 8) Exactly
std::distance(first1, last1)
applications ofunary_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 otherExecutionPolicy
, 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:
#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
Hover to see the original license.