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 起) |
[first1, last1) 的投影值等于范围 [first2, last2) 的投影值则返回 true,否则返回 false。r 为源范围,如同以 ranges::begin(r) 为 first 并以 ranges::end(r) 为 last。若两个范围拥有相同数量的元素,且每对投影元素都满足 pred,即 std::invoke(pred, std::invoke(proj1, *first1), std::invoke(proj2, *first2)) 对范围中的所有对应元素对都返回 true,则认为它们相等。
此页面上描述的函数式实体是算法函数对象(非正式地称为 niebloid),即:
参数
| 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;
|
示例
下列代码用 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) |
查找首个满足特定条件的元素 (算法函数对象) |
当一个范围字典序小于另一个时返回 true (算法函数对象) | |
(C++20) |
查找两个范围的首个不同之处 (算法函数对象) |
(C++20) |
搜索元素范围的首次出现 (算法函数对象) |
(C++20) |
返回匹配特定键值的元素范围 (算法函数对象) |
实现 x == y 的函数对象 (类模板) | |
| 判断两组元素是否相同 (函数模板) |