std::mismatch
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| ヘッダ <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) によって定義される2つの範囲の、一致しない最初の要素の組を返します。 last2 が提供されない場合 (オーバーロード (1-4)) は、 first2 + (last1 - first1) が使用されます。
1,5) 要素は
operator== を使用して比較されます。3,7) 要素は指定された二項述語
p を使用して比較されます。2,4,6,8) (1,3,5,7) おt同じですが、
policy に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します引数
| first1, last1 | - | 1つめの要素の範囲 |
| first2, last2 | - | 2つめの要素の範囲 |
| policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
| p | - | 要素が等しいと扱われるべき場合に true を返す二項述語。 述語関数のシグネチャは以下と同等なものであるべきです。
シグネチャが |
| 型の要件 | ||
-InputIt1 は LegacyInputIterator の要件を満たさなければなりません。
| ||
-InputIt2 は LegacyInputIterator の要件を満たさなければなりません。
| ||
-ForwardIt1 は LegacyForwardIterator の要件を満たさなければなりません。
| ||
-ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。
| ||
-BinaryPredicate は BinaryPredicate の要件を満たさなければなりません。
| ||
戻り値
等しくない最初の2つの要素を指すイテレータの std::pair。
比較が last1 に達しても不一致が見つからない場合、 pair は last1 と、2つめの範囲の対応するイテレータを保持します。 2つめの範囲が1つめの範囲より短い場合、動作は未定義です。
|
(C++14未満) |
比較が last1 または last2 いずれか先に来る方に達しても不一致が見つからない場合、 pair はその終端イテレータと、他方の範囲の対応するイテレータを保持します。
|
(C++14以上) |
計算量
1-4) 多くとも
last1 - first1 回の operator== または述語 p の適用。5-8) 多くとも min(
last1 - first1, last2 - first2) 回の operator== または述語 p の適用。例外
テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicyが標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicyについては、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
実装例
| 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);
}
|
| 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);
}
|
| 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);
}
|
| 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);
}
|
例
このプログラムは、指定された文字列の先頭と終端に同時に逆順に見つかる最長の部分文字列 (オーバーラップする可能性もあります) を決定します。
Run this code
#include <iostream>
#include <string>
#include <algorithm>
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
関連項目
| 2つの要素集合が同じかどうか調べます (関数テンプレート) | |
(C++11) |
一定の基準を満たす最初の要素を探します (関数テンプレート) |
| ある範囲が別の範囲より辞書的に小さいかどうか調べます (関数テンプレート) | |
| 指定範囲の要素に対して検索を行います (関数テンプレート) |