名前空間
変種
操作

std::mismatch

From cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)
mismatch

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(C++17)

ソートおよび関連操作
パーティション操作
ソート操作
二分探索操作
(パーティション化された範囲)
集合操作 (ソート済み範囲)
マージ操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書順比較操作
順列操作
Cライブラリ
数値演算
未初期化メモリに対する操作
 
ヘッダー <algorithm> で定義
template< class InputIt1, class InputIt2 >

std::pair<InputIt1, InputIt2>
    mismatch( InputIt1 first1, InputIt1 last1,

              InputIt2 first2 );
(1) (C++20 以降 constexpr)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

std::pair<ForwardIt1, ForwardIt2>
    mismatch( ExecutionPolicy&& policy,
              ForwardIt1 first1, ForwardIt1 last1,

              ForwardIt2 first2 );
(2) (C++17以降)
template< class InputIt1, class InputIt2, class BinaryPred >

std::pair<InputIt1, InputIt2>
    mismatch( InputIt1 first1, InputIt1 last1,

              InputIt2 first2, BinaryPred p );
(3) (C++20 以降 constexpr)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class BinaryPred >
std::pair<ForwardIt1, ForwardIt2>
    mismatch( ExecutionPolicy&& policy,
              ForwardIt1 first1, ForwardIt1 last1,

              ForwardIt2 first2, BinaryPred p );
(4) (C++17以降)
template< class InputIt1, class InputIt2 >

std::pair<InputIt1, InputIt2>
    mismatch( InputIt1 first1, InputIt1 last1,

              InputIt2 first2, InputIt2 last2 );
(5) (C++14以降)
(C++20 以降 constexpr)
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以降)
template< class InputIt1, class InputIt2, class BinaryPred >

std::pair<InputIt1, InputIt2>
    mismatch( InputIt1 first1, InputIt1 last1,

              InputIt2 first2, InputIt2 last2, BinaryPred p );
(7) (C++14以降)
(C++20 以降 constexpr)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class BinaryPred >
std::pair<ForwardIt1, ForwardIt2>
    mismatch( ExecutionPolicy&& policy,
              ForwardIt1 first1, ForwardIt1 last1,

              ForwardIt2 first2, ForwardIt2 last2, BinaryPred p );
(8) (C++17以降)

[first1last1)first2 から始まる範囲の最初の不一致要素へのイテレータペアを返します。

  • オーバーロード (1-4) の場合、2 番目の範囲は std::distance(first1, last1) 個の要素を持ちます。
  • オーバーロード (5-8) の場合、2 番目の範囲は [first2last2) です。
  • std::distance(first1, last1)std::distance(first2, last2) が異なる場合、比較は last1 または last2 に到達したときに停止します。
1,5) 要素は operator== を使用して比較されます。
3,7) 要素は指定された二項述語 p を使用して比較されます。
2,4,6,8) (1,3,5,7) と同じですが、policy に従って実行されます。
これらのオーバーロードは、以下のすべての条件が満たされた場合にのみオーバーロード解決に参加する。

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>true である。

(C++20まで)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>true です。

(C++20以降)

目次

[編集] パラメータ

first1, last1 - 比較する最初の要素の範囲を定義するイテレータのペア
first2, last2 - 比較する2番目の要素の範囲を定義するイテレータのペア
policy - 使用する 実行ポリシー
p - 要素が等しいと見なされる場合に true を返す二項述語。

述語関数のシグネチャは、以下と同等である必要がある。

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

シグネチャは const & を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、値カテゴリに関わらず、(おそらく const の) Type1Type2 のすべての値を受け入れられる必要がある (したがって、Type1 & は許可されない。また、Type1 の場合、ムーブがコピーと同等でない限り、Type1 も許可されない。(C++11以降))。
Type1Type2 は、InputIt1InputIt2 型のオブジェクトが逆参照され、それぞれ Type1Type2 に暗黙的に変換できるものでなければなりません。

型要件
-
InputIt1LegacyInputIterator の要件を満たしている必要があります。
-
InputIt2LegacyInputIterator の要件を満たしている必要があります。
-
ForwardIt1LegacyForwardIterator の要件を満たしている必要があります。
-
ForwardIt2LegacyForwardIterator の要件を満たしている必要があります。
-
BinaryPredBinaryPredicate の要件を満たす必要がある。

[編集] 戻り値

最初の2つの等しくない要素へのイテレータを含む std::pair

last1 に到達した場合、ペアの2番目のイテレータは first2 から数えて std::distance(first1, last1)番目 のイテレータです。

オーバーロード (5-8) の場合、last2 に到達した場合、ペアの最初のイテレータは first1 から数えて std::distance(first2, last2)番目 のイテレータです。

[編集] 計算量

std::distance(first1, last1)N1 とし、std::distance(first2, last2)N2 とする。

1,2) operator== を使用した比較は最大 N1 回です。
3,4) 述語 p の適用は最大 N1 回です。
5,6) operator== を使用した比較は最大 min(N1,N2) 回です。
7,8) 述語 p の適用は最大 min(N1,N2) 回です。

[編集] 例外

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 BinaryPred>
std::pair<InputIt1, InputIt2>
    mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPred 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 BinaryPred>
std::pair<InputIt1, InputIt2>
    mismatch(InputIt1 first1, InputIt1 last1,
             InputIt2 first2, InputIt2 last2, BinaryPred 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

[編集] 関連項目

2つの要素の集合が同じかどうかを判断する
(関数テンプレート) [編集]
特定の基準を満たす最初の要素を見つける
(関数テンプレート) [編集]
ある範囲が別の範囲より辞書順で小さい場合に true を返す
(関数テンプレート) [編集]
要素の範囲の最初の出現を検索する
(関数テンプレート) [編集]
2つの範囲が異なる最初の位置を見つける
(アルゴリズム関数オブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)