Namespaces
Variants

std:: mismatch

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11) (C++11) (C++11)
mismatch

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
HTMLタグ、属性、C++コード内のテキストは翻訳せず、元のフォーマットを保持しました。C++20以降でconstexprが利用可能であることを示す注記のみを日本語で表現しています。
定義済みヘッダ <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) (constexpr since C++20)
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以降)

[ first1 , last1 ) の要素と first2 から始まる範囲の要素間で、最初に不一致が見つかった位置のイテレータのペアを返します:

  • オーバーロード (1-4) の場合、第2の範囲は std:: distance ( first1, last1 ) 個の要素を持ちます。
  • オーバーロード (5-8) の場合、第2の範囲は [ first2 , last2 ) です。
  • 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 & を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、値カテゴリに関係なく Type1 Type2 のすべての値を受け入れられる必要があります(したがって、 Type1 & は許可されません 、また Type1 も、 Type1 に対してムーブがコピーと等価でない限り許可されません (C++11以降) )。
Type1 Type2 は、 InputIt1 InputIt2 のオブジェクトが間接参照可能であり、それぞれ Type1 Type2 に暗黙的に変換可能である必要があります。 ​

型要件
-
InputIt1 LegacyInputIterator の要件を満たさなければなりません。
-
InputIt2 LegacyInputIterator の要件を満たさなければなりません。
-
ForwardIt1 LegacyForwardIterator の要件を満たさなければなりません。
-
ForwardIt2 LegacyForwardIterator の要件を満たさなければなりません。
-
BinaryPred BinaryPredicate の要件を満たさなければなりません。

戻り値

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

last1 に達した場合、ペアの2番目のイテレータは std:: distance ( first1, last1 ) 番目の first2 以降のイテレータとなる。

オーバーロード (5-8) の場合、 last2 に到達したとき、ペア内の最初のイテレータは std:: distance ( first2, last2 ) 番目の イテレータであり、 first1 の後ろに位置します。

計算量

N 1 std:: distance ( first1, last1 ) として、 N 2 std:: distance ( first2, last2 ) として定義する:

1,2) 最大 N 1 回の比較を operator == を使用して行う。
3,4) 最大 N 1 回の述語 p の適用。
5,6) 最大 min(N 1 ,N 2 ) 回の比較を operator == を使用して行う。
7,8) 最大 min(N 1 ,N 2 ) 回の述語 p の適用。
**翻訳の説明:** - 「At most」→「最大」に翻訳 - 「applications of the predicate」→「述語の適用」に翻訳 - HTMLタグ、属性、数式部分(min(N₁,N₂))、C++コード(p)はそのまま保持 - 数値の下付き文字(N₁, N₂)はHTML構造を維持 - プロフェッショナルな技術文書として正確な翻訳を実施

例外

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つの範囲が最初に異なる位置を見つける
(アルゴリズム関数オブジェクト)