The Wayback Machine - https://web.archive.org/web/20191216131154/https://ja.cppreference.com/w/cpp/algorithm/mismatch
名前空間
変種
操作

std::mismatch

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
コンセプトとユーティリティ (std::Sortable, std::projected, ... )
制約付きアルゴリズム (std::ranges::copy, std::ranges::sort, ... )
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
二分探索操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)

順列
数値演算
C のライブラリ
 
ヘッダ <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 >

constexprstd::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 >

constexprstd::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 >

constexprstd::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 >

constexprstd::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 を返す二項述語。

述語関数のシグネチャは以下と同等なものであるべきです。

 bool pred(const Type1 &a, const Type2 &b);

シグネチャが const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなならず、値カテゴリに関わらず Type1 および Type2 型 (およびそれらの const 修飾された型) のすべての値を受理できなければなりません (そのため Type1 & は許されません。 また Type1 に対してムーブがコピーと同等でなければ Type1 も許されません(C++11以上))。
Type1 および Type2 は、 InputIt1 および InputIt2 型のオブジェクトの逆参照からType1 および Type2 にそれぞれ暗黙に変換可能なものでなければなりません。 ​

型の要件
-
InputIt1LegacyInputIterator の要件を満たさなければなりません。
-
InputIt2LegacyInputIterator の要件を満たさなければなりません。
-
ForwardIt1LegacyForwardIterator の要件を満たさなければなりません。
-
ForwardIt2LegacyForwardIterator の要件を満たさなければなりません。
-
BinaryPredicateBinaryPredicate の要件を満たさなければなりません。

[編集]戻り値

等しくない最初の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;}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つの要素集合が同じかどうか調べます
(関数テンプレート)[edit]
一定の基準を満たす最初の要素を探します
(関数テンプレート)[edit]
ある範囲が別の範囲より辞書的に小さいかどうか調べます
(関数テンプレート)[edit]
指定範囲の要素に対して検索を行います
(関数テンプレート)[edit]
close