Namespaces
Variants

std:: equal

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
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
ヘッダーで定義 <algorithm>
template < class InputIt1, class InputIt2 >

bool equal ( InputIt1 first1, InputIt1 last1,

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

bool equal ( ExecutionPolicy && policy,
ForwardIt1 first1, ForwardIt1 last1,

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

bool equal ( InputIt1 first1, InputIt1 last1,

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

class ForwardIt1, class ForwardIt2, class BinaryPred >
bool equal ( ExecutionPolicy && policy,
ForwardIt1 first1, ForwardIt1 last1,

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

bool equal ( InputIt1 first1, InputIt1 last1,

InputIt2 first2, InputIt2 last2 ) ;
(5) (since C++14)
(constexpr since C++20)
template < class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

bool equal ( ExecutionPolicy && policy,
ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2, ForwardIt2 last2 ) ;
(6) (since C++17)
template < class InputIt1, class InputIt2, class BinaryPred >

bool equal ( InputIt1 first1, InputIt1 last1,

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

class ForwardIt1, class ForwardIt2, class BinaryPred >
bool equal ( ExecutionPolicy && policy,
ForwardIt1 first1, ForwardIt1 last1,

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

[ first1 , last1 ) の範囲と first2 から始まる範囲が等しいかどうかをチェックします:

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

型要件
-
InputIt1, InputIt2 LegacyInputIterator の要件を満たさなければならない。
-
ForwardIt1, ForwardIt2 LegacyForwardIterator の要件を満たさなければならない。
-
BinaryPred BinaryPredicate の要件を満たさなければならない。

戻り値

1-4) 2つの範囲の対応する各要素が等しい場合、 true を返す。それ以外の場合、 false を返す。
5-8) もし std:: distance ( first1, last1 ) std:: distance ( first2, last2 ) が等しく、かつ2つの範囲の対応する各要素がすべて等しい場合、 true を返す。それ以外の場合 false を返す。

計算量

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

1) 最大 N 1 回の比較を operator == を使用して行う。
2) O(N 1 ) 回の比較を operator == を使用して行う。
3) 最大で N 1 回の述語 p の適用。
4) O(N 1 ) 個の述語 p の適用。
5-8) InputIt1 InputIt2 が両方とも LegacyRandomAccessIterator であり、かつ last1 - first1 ! = last2 - first2 true の場合、比較は行われません。
それ以外の場合、 N min(N 1 ,N 2 ) として定義する:
5) 最大 N 回の比較を operator == を使用して行う。
6) O(N) 回の比較を operator == を使用して行う。
7) 最大で N 回の述語 p の適用。
8) O(N) 回の述語 p の適用。

例外

ExecutionPolicy という名前のテンプレートパラメータを持つオーバーロードは、 以下のようにエラーを報告します:

  • アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、 ExecutionPolicy 標準ポリシー のいずれかであるとき、 std::terminate が呼び出される。それ以外の ExecutionPolicy については、動作は実装定義である。
  • アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされる。

実装例

このHTMLコードは既に日本語環境で使用可能な形式です。以下の点に注意しています: - HTMLタグと属性は翻訳せず保持 - リンクテキスト「equal (1)」はC++用語のため翻訳せず保持 - 元のフォーマットと構造を完全に維持 変更点はありません。 HTMLタグ、属性、コードブロック内のテキストは翻訳せず、元のフォーマットを保持しました。C++固有の用語も翻訳していません。 **日本語訳:** **翻訳説明:** - "equal" を「イコール」と翻訳(C++の関数名として一般的な表記) - "(3)" はバージョン番号を示すため、そのまま保持 - HTMLタグ、属性、構造は完全に保持 - フォーマットとインデントを元のまま維持 HTMLタグ、属性、コードブロック内のテキストは翻訳せず、元のフォーマットを保持しました。C++固有の用語も翻訳していません。
equal (1)
template<class InputIt1, class InputIt2>
constexpr //< since C++20
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
{
    for (; first1 != last1; ++first1, ++first2)
        if (!(*first1 == *first2))
            return false;
    return true;
}
equal (3)
イコール (3)
template<class InputIt1, class InputIt2, class BinaryPred>
constexpr //< since C++20
bool equal(InputIt1 first1, InputIt1 last1,
           InputIt2 first2, BinaryPred p)
{
    for (; first1 != last1; ++first1, ++first2)
        if (!p(*first1, *first2))
            return false;
    return true;
}
equal (5)
namespace detail
{
    // ランダムアクセスイテレータの実装(範囲サイズの高速検出を可能にする)
    template<class RandomIt1, class RandomIt2>
    constexpr //< C++20以降
    bool equal(RandomIt1 first1, RandomIt1 last1, RandomIt2 first2, RandomIt2 last2,
               std::random_access_iterator_tag, std::random_access_iterator_tag)
    {
        if (last1 - first1 != last2 - first2)
            return false;
        for (; first1 != last1; ++first1, ++first2)
            if (!(*first1 == *first2))
                return false;
        return true;
    }
    // 入力イテレータの実装(「last2」との手動比較が必要)
    template<class InputIt1, class InputIt2>
    constexpr //< C++20以降
    bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
               std::input_iterator_tag, std::input_iterator_tag)
    {
        for (; first1 != last1 && first2 != last2; ++first1, ++first2)
            if (!(*first1 == *first2))
                return false;
        return first1 == last1 && first2 == last2;
    }
}
template<class InputIt1, class InputIt2>
constexpr //< C++20以降
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
{
    details::equal(first1, last1, first2, last2,
                   typename std::iterator_traits<InputIt1>::iterator_category(),
                   typename std::iterator_traits<InputIt2>::iterator_category());
}
equal (7)
namespace detail
{
    // ランダムアクセスイテレータの実装(範囲サイズの高速検出を可能にする)
    template<class RandomIt1, class RandomIt2, class BinaryPred>
    constexpr //< C++20以降
    bool equal(RandomIt1 first1, RandomIt1 last1,
               RandomIt2 first2, RandomIt2 last2, BinaryPred p,
               std::random_access_iterator_tag, std::random_access_iterator_tag)
    {
        if (last1 - first1 != last2 - first2)
            return false;
        for (; first1 != last1; ++first1, ++first2)
            if (!p(*first1, *first2))
                return false;
        return true;
    }
    // 入力イテレータの実装(「last2」との手動比較が必要)
    template<class InputIt1, class InputIt2, class BinaryPred>
    constexpr //< C++20以降
    bool equal(InputIt1 first1, InputIt1 last1,
               InputIt2 first2, InputIt2 last2, BinaryPred p,
               std::input_iterator_tag, std::input_iterator_tag)
    {
        for (; first1 != last1 && first2 != last2; ++first1, ++first2)
            if (!p(*first1, *first2))
                return false;
        return first1 == last1 && first2 == last2;
    }
}
template<class InputIt1, class InputIt2, class BinaryPred>
constexpr //< C++20以降
bool equal(InputIt1 first1, InputIt1 last1,
           InputIt2 first2, InputIt2 last2, BinaryPred p)
{
    details::equal(first1, last1, first2, last2, p,
                   typename std::iterator_traits<InputIt1>::iterator_category(),
                   typename std::iterator_traits<InputIt2>::iterator_category());
}

注記

std::equal は、 std::unordered_set std::unordered_multiset std::unordered_map 、または std::unordered_multimap のイテレータによって形成される範囲を比較するために使用すべきではありません。なぜなら、これらのコンテナに格納される要素の順序は、たとえ2つのコンテナが同じ要素を格納している場合でも異なる可能性があるためです。

コンテナ全体 または文字列ビュー (C++17以降) の等価性を比較する場合、 operator == が通常推奨されます。

シーケンシャルな std::equal は短絡評価が保証されていません。例えば、両範囲の最初の要素ペアが等しく比較されない場合でも、残りの要素も比較される可能性があります。非短絡比較は、範囲が std::memcmp または実装固有のベクトル化アルゴリズムで比較される場合に発生する可能性があります。

以下のコードは std::equal を使用して、文字列が回文かどうかをテストします。

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string_view>
constexpr bool is_palindrome(const std::string_view& s)
{
    return std::equal(s.cbegin(), s.cbegin() + s.size() / 2, s.crbegin());
}
void test(const std::string_view& s)
{
    std::cout << std::quoted(s)
              << (is_palindrome(s) ? " is" : " is not")
              << " a palindrome\n";
}
int main()
{
    test("radar");
    test("hello");
}

出力:

"radar" is a palindrome
"hello" is not a palindrome

関連項目

特定の条件を満たす最初の要素を見つける
(関数テンプレート)
一方の範囲が他方よりも辞書順で小さい場合に true を返す
(関数テンプレート)
二つの範囲が最初に異なる位置を見つける
(関数テンプレート)
要素の範囲の最初の出現を検索する
(関数テンプレート)
二つの要素の集合が同じかどうかを判定する
(アルゴリズム関数オブジェクト)
x == y を実装する関数オブジェクト
(クラステンプレート)
特定のキーに一致する要素の範囲を返す
(関数テンプレート)