std::mismatch
Определено в заголовке <algorithm> | ||
|---|---|---|
| (1) | ||
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2 ); | (до C++20) | |
template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2 );
| (с 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) |
| (3) | ||
template< class InputIt1, class InputIt2, class BinaryPredicate >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2,
BinaryPredicate p ); | (до C++20) | |
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2,
BinaryPredicate p );
| (с C++20) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class BinaryPredicate >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2,
BinaryPredicate p );
| (4) | (с C++17) |
| (5) | ||
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 ); |
(с C++14) (до C++20) | |
template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 );
| (с C++20) | |
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) |
| (7) | ||
template< class InputIt1, class InputIt2, class BinaryPredicate >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
BinaryPredicate p ); |
(с C++14) (до C++20) | |
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
BinaryPredicate p );
| (с C++20) | |
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 );
| (8) | (с C++17) |
Возвращает первую пару несовпадающих элементов из двух диапазонов: одного, определённого [first1, last1) и другого, определённого [first2, last2). Если last2 не указана (перегрузки (1-4)), она обозначает first2 + (last1 - first1).
operator==.p.policy. Эти перегрузки не участвуют в разрешении перегрузки, если |
| (до C++20) |
|
| (с C++20) |
Параметры
| first1, last1 | - | первый диапазон элементов |
| first2, last2 | - | второй диапазон элементов |
| policy | - | используемая политика выполнения. Подробнее см. политика выполнения. |
| p | - | бинарный предикат, возвращающий true, если элементы должны обрабатываться как равные. Подпись функции-предиката должна быть эквивалентна следующей:
Хотя подпись не должна содержать |
| Требования к типу | ||
-InputIt1 должно удовлетворять требованиям LegacyInputIterator. |
||
-InputIt2 должно удовлетворять требованиям LegacyInputIterator. |
||
-ForwardIt1 должно удовлетворять требованиям LegacyForwardIterator. |
||
-ForwardIt2 должно удовлетворять требованиям LegacyForwardIterator. |
||
-BinaryPredicate должно удовлетворять требованиям BinaryPredicate. |
||
Возвращаемое значение
std::pair с итераторами на первые два неравных элемента.
| Если несовпадений не найдено, когда сравнение достигает | (до C++14) |
| Если несовпадений не найдено, когда сравнение достигает | (с C++14) |
Сложность
last1 - first1 применений 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 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 (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 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);
} |
Пример
Эта программа определяет самую длинную подстроку, которая одновременно встречается в начале заданной строки и в конце её, в обратном порядке (возможно, перекрывающуюся).
#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++11) | находит первый элемент, удовлетворяющий определённым критериям (шаблон функции) |
возвращает true, если один диапазон лексикографически меньше другого (шаблон функции) |
|
| выполняет поиск диапазона элементов (шаблон функции) |
|
|
(C++20) | находит первую позицию, где два диапазона различаются (niebloid) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/mismatch