Namespaces
Variants

std::ranges::mismatch, std::ranges::mismatch_result

ja.cppreference.net より
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
ソートと関連操作
分割操作
(C++11)    

ソート操作
二分探索操作
(分割済み範囲に対する)
集合操作 (ソート済み範囲に対する)
マージ操作 (ソート済み範囲に対する)
ヒープ操作
最小値/最大値操作
(C++11)
(C++17)
辞書比較操作
順列操作


 
制約付きアルゴリズム
このメニューのすべての名前は名前空間 std::ranges
に属します非変更シーケンス操作
変更シーケンス操作
分割操作
ソート操作
二分探索操作 (ソート済み範囲に対する)
       
       
集合操作 (ソート済み範囲に対する)
ヒープ操作
最小値/最大値操作
       
       
順列操作
畳み込み操作
数値操作
(C++23)            
未初期化記憶域に対する操作
戻り値の型
 
ヘッダ <algorithm>
で定義
template< std::input_iterator I1, std::sentinel_for<I1> S1,
          std::input_iterator I2, std::sentinel_for<I2> S2,
          class Pred = ranges::equal_to,
          class Proj1 = std::identity, class Proj2 = std::identity >
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr mismatch_result<I1, I2>
    mismatch( I1 first1, S1 last1, I2 first2, S2 last2,
              Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
呼び出しシグネチャ (1)
template< ranges::input_range R1, ranges::input_range R2,
          class Pred = ranges::equal_to,
          class Proj1 = std::identity, class Proj2 = std::identity >
requires std::indirectly_comparable<
             ranges::iterator_t<R1>, ranges::iterator_t<R2>, Pred, Proj1, Proj2>
constexpr mismatch_result<ranges::borrowed_iterator_t<R1>,
                          ranges::borrowed_iterator_t<R2>>
    mismatch( R1&& r1, R2&& r2, Pred pred = {},
              Proj1 proj1 = {}, Proj2 proj2 = {} );
(C++20以上) (2)
(C++20以上)
template< class I1, class I2 >
using mismatch_result = ranges::in_in_result<I1, I2>;
ヘルパー型 (3)

(C++20以上)[first1, last1) または r1 で定義された1つの範囲と、 [first2, last2) または r2 で定義された別の範囲の2つの範囲から、射影された要素の最初の不一致ペアを返します。

1) 要素は指定された二項述語 p を使用して比較されます。
2) (1) と同じですが、ソース範囲として r を使用する点が異なります。これは、ranges::begin(r) を first として、ranges::end(r) を last として使用するのと同じです。

このページで説明されている関数のようなエンティティは アルゴリズム関数オブジェクト (非公式に niebloid として知られています) であり、つまり:

  • それらのいずれかを呼び出すときに、明示的なテンプレート引数リストを指定することはできません。
  • それらのいずれも 引数依存の名前探索 からは見えません。
  • それらのいずれかが 通常の非修飾名前探索 によって関数呼び出し演算子の左側の名前として見つかった場合、引数依存の名前探索 は抑制されます。

パラメータ

first1, last1 - 比較対象の最初の要素範囲を定義する 範囲 のイテレータ-センチネルペア
r1 - 比較対象の最初の要素範囲
first2, last2 - 比較対象の2番目の要素範囲を定義する 範囲 のイテレータ-センチネルペア
r2 - 比較対象の2番目の要素範囲
pred - 投影された要素に適用する述語
proj1 - 最初の要素範囲に適用する投影
proj2 - 2番目の要素範囲に適用する投影

戻り値

ranges::mismatch_result 最初の2つの非等価要素へのイテレータ。

比較が last1 または last2 のいずれかに最初に到達した時点で不一致が見つからなかった場合、オブジェクトは終端イテレータと他方の範囲からの対応するイテレータを保持します。

計算量

最大で std:: min ( last1 - first1, last2 - first2 ) 回の述語と対応する射影の適用。

実装例

struct mismatch_fn
{
    template<std::input_iterator I1, std::sentinel_for<I1> S1,
             std::input_iterator I2, std::sentinel_for<I2> S2,
             class Pred = ranges::equal_to,
             class Proj1 = std::identity, class Proj2 = std::identity>
    requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
    constexpr std::mismatch_result<I1, I2>
        operator()(I1 first1, S1 last1, I2 first2, S2 last2,
                   Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        for (; first1 != last1 && first2 != last2; ++first1, (void)++first2)
            if (not std::invoke(pred, std::invoke(proj1, *first1),
                                      std::invoke(proj2, *first2)))
                break;
        return {first1, first2};
    }
    template<ranges::input_range R1, ranges::input_range R2,
             class Pred = ranges::equal_to,
             class Proj1 = std::identity, class Proj2 = std::identity>
    requires std::indirectly_comparable<ranges::iterator_t<R1>, ranges::iterator_t<R2>,
                                        Pred, Proj1, Proj2>
    constexpr ranges::mismatch_result<ranges::borrowed_iterator_t<R1>,
                                      ranges::borrowed_iterator_t<R2>>
        operator()(R1&& r1, R2&& r2, Pred pred = {},
                   Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        return (*this)(ranges::begin(r1), ranges::end(r1),
                       ranges::begin(r2), ranges::end(r2),
                       std::ref(pred), std::ref(proj1), std::ref(proj2));
    }
};
inline constexpr mismatch_fn mismatch;

例

このプログラムは、与えられた文字列の先頭と末尾で同時に見つかる最長の部分文字列を決定します(逆順で、重複する可能性があります)。

#include <algorithm>
#include <iostream>
#include <ranges>
#include <string_view>
[[nodiscard]]
constexpr std::string_view mirror_ends(const std::string_view in)
{
    const auto end = std::ranges::mismatch(in, in | std::views::reverse).in1;
    return {in.cbegin(), end};
}
int main()
{
    std::cout << mirror_ends("abXYZba") << '\n'
              << mirror_ends("abca") << '\n'
              << mirror_ends("ABBA") << '\n'
              << mirror_ends("level") << '\n';
    using namespace std::literals::string_view_literals;
    static_assert("123"sv == mirror_ends("123!@#321"));
    static_assert("radar"sv == mirror_ends("radar"));
}

出力:

ab
a
ABBA
level

関連項目

2つの要素集合が同じであるかを判定する
(アルゴリズム関数オブジェクト)
特定の条件を満たす最初の要素を発見する
(アルゴリズム関数オブジェクト)
2つの範囲を辞書的に比較する
(アルゴリズム関数オブジェクト)
要素の範囲の最初の出現位置を検索する
(アルゴリズム関数オブジェクト)
2つの範囲が異なる最初の位置を発見する
(関数テンプレート)