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) (自 C++20 起为 constexpr)
template < class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

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

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

bool equal ( InputIt1 first1, InputIt1 last1,

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

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

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

bool equal ( InputIt1 first1, InputIt1 last1,

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

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

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

bool equal ( 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 >
bool equal ( ExecutionPolicy && policy,
ForwardIt1 first1, ForwardIt1 last1,

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

检查 [ first1 , last1 ) 与起始于 first2 的范围是否相等:

  • 对于重载 (1-4) ,第二个范围包含 std:: distance ( first1, last1 ) 个元素。
  • 对于重载 (5-8) ,第二个范围为 [ 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 - 定义待比较的第二个 元素范围 的迭代器对
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) 如果两个范围中的每个对应元素都相等,则返回 true 。否则返回 false
5-8) std:: distance ( first1, last1 ) std:: distance ( first2, last2 ) 相等,且两个范围内的每个对应元素均相等,则返回 true ,否则返回 false

复杂度

给定 N 1 std:: distance ( first1, last1 ) N 2 std:: distance ( first2, last2 )

1) 最多使用 operator == 进行 N 1 次比较。
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

可能的实现

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)
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 //< since 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 //< since 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 //< since 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 //< since 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 //< since 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 &&</

注释

std::equal 不应被用于比较由 std::unordered_set std::unordered_multiset std::unordered_map std::unordered_multimap 的迭代器所构成的范围,因为即使两个容器存储的元素相同,这些容器中元素的存储顺序也可能不同。

当比较整个容器 或字符串视图 (自 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 的函数对象
(类模板)
返回匹配特定键的元素范围
(函数模板)