std::mismatch
ヘッダ <algorithm> で定義 | ||
(1) | ||
template<class InputIt1, class InputIt2 > std::pair<InputIt1,InputIt2> | (C++20未満) | |
template<class InputIt1, class InputIt2 > constexprstd::pair<InputIt1,InputIt2> | (C++20以上) | |
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > std::pair<ForwardIt1,ForwardIt2> | (2) | (C++17以上) |
(3) | ||
template<class InputIt1, class InputIt2, class BinaryPredicate > std::pair<InputIt1,InputIt2> | (C++20未満) | |
template<class InputIt1, class InputIt2, class BinaryPredicate > constexprstd::pair<InputIt1,InputIt2> | (C++20以上) | |
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > std::pair<ForwardIt1,ForwardIt2> | (4) | (C++17以上) |
(5) | ||
template<class InputIt1, class InputIt2 > std::pair<InputIt1,InputIt2> | (C++14以上) (C++20未満) | |
template<class InputIt1, class InputIt2 > constexprstd::pair<InputIt1,InputIt2> | (C++20以上) | |
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > std::pair<ForwardIt1,ForwardIt2> | (6) | (C++17以上) |
(7) | ||
template<class InputIt1, class InputIt2, class BinaryPredicate > std::pair<InputIt1,InputIt2> | (C++14以上) (C++20未満) | |
template<class InputIt1, class InputIt2, class BinaryPredicate > constexprstd::pair<InputIt1,InputIt2> | (C++20以上) | |
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > std::pair<ForwardIt1,ForwardIt2> | (8) | (C++17以上) |
[first1, last1)
および [first2,last2)
によって定義される2つの範囲の、一致しない最初の要素の組を返します。 last2
が提供されない場合 (オーバーロード (1-4)) は、 first2 + (last1 - first1)
が使用されます。
operator==
を使用して比較されます。p
を使用して比較されます。policy
に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します目次 |
[編集]引数
first1, last1 | - | 1つめの要素の範囲 |
first2, last2 | - | 2つめの要素の範囲 |
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
p | - | 要素が等しいと扱われるべき場合に true を返す二項述語。 述語関数のシグネチャは以下と同等なものであるべきです。 bool pred(const Type1 &a, const Type2 &b); シグネチャが const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなならず、値カテゴリに関わらず |
型の要件 | ||
-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以上) |
[編集]計算量
last1
- first1
回の operator==
または述語 p
の適用。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;}returnstd::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;}returnstd::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;}returnstd::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;}returnstd::make_pair(first1, first2);} |
[編集]例
このプログラムは、指定された文字列の先頭と終端に同時に逆順に見つかる最長の部分文字列 (オーバーラップする可能性もあります) を決定します。
#include <iostream>#include <string>#include <algorithm> std::string mirror_ends(conststd::string& in){returnstd::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) | 一定の基準を満たす最初の要素を探します (関数テンプレート) |
ある範囲が別の範囲より辞書的に小さいかどうか調べます (関数テンプレート) | |
指定範囲の要素に対して検索を行います (関数テンプレート) |