Namespaces
Variants

std::ranges:: 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
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
定义于头文件 <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 bool
equal ( I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = { } ,

Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(1) (C++20 起)
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 bool

equal ( R1 && r1, R2 && r2, Pred pred = { } , Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(2) (C++20 起)
1) 若范围 [ first1 , last1 ) 的投影值等于范围 [ first2 , last2 ) 的投影值,则返回 true ;否则返回 false
2) (1) 相同,但使用 r 作为源范围,如同使用 ranges:: begin ( r ) 作为 first ,以及 ranges:: end ( r ) 作为 last

两个范围被视为相等,当它们具有相同数量的元素,且每对对应的投影元素均满足 pred 。即对于两个范围中所有对应的元素对, std:: invoke ( pred, std:: invoke ( proj1, * first1 ) , std:: invoke ( proj2, * first2 ) ) 均返回 true

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first1, last1 - 定义待比较第一个 范围 元素的迭代器-哨位对
r1 - 待比较元素的第一个范围
first2, last2 - 定义待比较第二个 范围 元素的迭代器-哨位对
r2 - 待比较元素的第二个范围
pred - 应用于投影元素的二元谓词
proj1 - 应用于第一个元素范围的投影
proj2 - 应用于第二个元素范围的投影

返回值

如果范围 [ first1 , last1 ) 的长度不等于范围 [ first2 , last2 ) 的长度,则返回 false

如果两个范围内的元素在投影后相等,则返回 true

否则返回 false

注释

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

当比较整个容器或字符串视图是否相等时,通常推荐使用对应类型的 operator ==

ranges::equal 不保证短路求值。例如,如果两个范围的第一个元素对比较不相等,仍可能继续比较剩余元素。当使用 std::memcmp 或实现特定的向量化算法比较范围时,可能发生非短路比较。

复杂度

最多进行 min ( last1 - first1, last2 - first2 ) 次谓词及对应投影函数的调用。

然而,如果 S1 S2 都对其各自的迭代器建模 std::sized_sentinel_for ,且 last1 - first1 ! = last2 - first2 ,则不会执行任何谓词调用(无需查看任何元素即可检测到大小不匹配)。

可能的实现

struct equal_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 bool
      operator()(I1 first1, S1 last1, I2 first2, S2 last2,
                 Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
  {
      if constexpr (std::sized_sentinel_for<S1, I1> and std::sized_sentinel_for<S2, I2>)
          if (std::ranges::distance(first1, last1) != std::ranges::distance(first2, last2))
              return false;
      for (; first1 != last1; ++first1, (void)++first2)
          if (!std::invoke(pred, std::invoke(proj1, *first1), std::invoke(proj2, *first2)))
              return false;
      return true;
  }
  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 bool
      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 equal_fn equal;

示例

以下代码使用 ranges::equal 来测试字符串是否为回文。

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <ranges>
#include <string_view>
constexpr bool is_palindrome(const std::string_view s)
{
    namespace views = std::views;
    auto forward = s | views::take(s.size() / 2);
    auto backward = s | views::reverse | views::take(s.size() / 2);
    return std::ranges::equal(forward, backward);
}
void test(const std::string_view s)
{
    std::cout << std::quoted(s) << " is "
              << (is_palindrome(s) ? "" : "not ")
              << "a palindrome\n";
}
int main()
{
    test("radar");
    test("hello");
    static_assert(is_palindrome("ABBA") and not is_palindrome("AC/DC"));
}

输出:

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

参见

查找满足特定条件的首个元素
(算法函数对象)
若一个范围按字典序小于另一个范围则返回 true
(算法函数对象)
查找两个范围首次出现不同的位置
(算法函数对象)
搜索元素范围的首次出现
(算法函数对象)
返回匹配特定键的元素范围
(算法函数对象)
实现 x == y 的函数对象
(类模板)
判断两组元素是否相同
(函数模板)