std::mismatch

提供:cppreference.com
移動: 案内, 検索

テンプレート:cpp/algorithm/sidebar

Defined in header <algorithm>
template< class InputIterator1, class InputIterator2 >

std::pair<InputIterator1,InputIterator2>
    mismatch( InputIterator1 first1,
              InputIterator1 last1,

              InputIterator2 first2 );
(1)
template< class InputIterator1, class InputIterator2, class BinaryPredicate >

std::pair<InputIterator1,InputIterator2>
    mismatch( InputIterator1 first1,
              InputIterator1 last1,
              InputIterator2 first2,

              BinaryPredicate p );
(2)

Returns the first mismatching pair of elements from two ranges: one defined by [first1, last1) and another starting at first2. The first version of the function uses operator== to compare the elements, the second version uses the given binary predicate p.

目次

[編集] 引数

first1, last1 - 1つめの比較される要素の範囲
first2 - 2つめの比較される要素の範囲の先頭
p - 要素が等しいと扱われるべき場合に ​true を返す2項述語.

述語のシグネチャは以下と等価である必要がある:

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

シグネチャは const & である必要はないが、この述語は渡されたオブジェクトを変更してはならない。
The types  Type1 and  Type2 must be such that objects of types InputIterator1 and InputIterator2 can be dereferenced and then implicitly converted to  Type1 and  Type2 respectively.

[編集] 返値

最初の等価でない要素のイテレータから成る std::pair を返す。等価でない要素が見つからなかった場合、last1 と、対応する2つめの範囲のイテレータからなる std::pair を返す。

[編集] 計算量

At most last1 - first1 applications of the predicate

[編集] 等価コード

[編集]

This program determines the the longest substring that is simultaneously found at the very beginning of the given string and at the very end of it, in reverse order (possibly overlapping)

#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';
}

[編集] See also

テンプレート:cpp/algorithm/dcl list equalテンプレート:cpp/algorithm/dcl list findテンプレート:cpp/algorithm/dcl list lexicographical compareテンプレート:cpp/algorithm/dcl list search
個人用ツール
名前空間
変種
操作
案内
ツールボックス
他の言語