std::ranges::equal
| 定义于头文件 <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 起) |
true,如果范围 [first1, last1) 的投影值等于范围 [first2, last2) 的投影值;否则返回 false。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
另见
(C++20)(C++20)(C++20) |
查找满足特定条件的第一个元素 (算法函数对象) |
| 按字典序比较两个范围 (算法函数对象) | |
(C++20) |
查找两个范围首次不同的位置 (算法函数对象) |
(C++20) |
搜索某个元素范围的首次出现 (算法函数对象) |
(C++20) |
使用二分查找查找匹配给定值的元素范围 (算法函数对象) |
实现x == y 的函数对象(类模板) | |
| 确定两个元素集是否相同 (函数模板) |