std::ranges::equal() 算法
- 自 C++20 起
- 简化
- 详细
// (1)
constexpr bool equal( I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
// (2)
constexpr bool equal( R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
参数类型是泛型的,并具有以下约束
I1
,I2
-std::input_iterator
S1
,S2
-std::sentinel_for<I1>
,std::sentinel_for<I2>
R1
,R2
-std::ranges::input_range
Pred
- (无)Proj1
,Proj2
- (无)
Proj1
、Proj2
和 Pred
模板参数具有以下默认类型:所有重载均为 std::identity
、ranges::less
。
此外,每个重载都有以下约束
- (1) -
std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
- (2) -
std::indirectly_comparable<ranges::iterator_t<R1>, ranges::iterator_t<R2>, Pred, Proj1, Proj2>
// (1)
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 = {} );
// (2)
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 = {} );
-
(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
。
复杂度
最多应用 min(last1 - first1, last2 - first2)
次谓词和相应的投影。
但是,如果 S1
和 S2
都对其各自的迭代器建模 std::sized_sentinel_for
,并且 last1 - first1 != last2 - first2
,则不会应用谓词(在不查看任何元素的情况下检测到大小不匹配)。
异常
(无)
可能的实现
ranges::equal
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 << 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