std::ranges::ends_with() 算法
- 自 C++20 起
- 简化
- 详细
// (1)
constexpr bool ends_with( I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
// (2)
constexpr bool ends_with( 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>
Pred
- (无)Proj1
,Proj2
- (无)- (2) -
R1
,R2
-std::ranges::input_range
Pred
模板参数对于所有重载都具有默认类型 ranges::equal_to
。Proj1
和 Proj2
模板参数对于所有重载都具有默认类型 std::identity
。
此外,每个重载都有以下约束
- (1):
(forward_iterator<I1> || sized_sentinel_for<S1, I1>)
&& (forward_iterator<I2> || sized_sentinel_for<S2, I2>)
&& indirectly_comparable<I1, I2, Pred, Proj1, Proj2>` - (2):
(ranges::forward_range<R1> || ranges::sized_range<R1>)
&& (ranges::forward_range<R2> || ranges::sized_range<R2>)
&& std::indirectly_comparable<ranges::iterator_t<R1>,
ranges::iterator_t<R2>,
Pred, Proj1, Proj2>
(为方便阅读,此处省略了 std::
命名空间)
// (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::forward_iterator<I1> || std::sized_sentinel_for<S1, I1>) &&
(std::forward_iterator<I2> || std::sized_sentinel_for<S2, I2>) &&
std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr bool ends_with( 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 (ranges::forward_range<R1> || ranges::sized_range<R1>) &&
(ranges::forward_range<R2> || ranges::sized_range<R2>) &&
std::indirectly_comparable<ranges::iterator_t<R1>,
ranges::iterator_t<R2>,
Pred, Proj1, Proj2>
constexpr bool ends_with( R1&& r1, R2&& r2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
检查第二个范围是否与第一个范围的后缀匹配。
-
(1) 比较是通过将二元谓词
pred
应用于分别由proj1
和proj2
投影的两个范围中的元素来完成的。 -
(2) 与 (1) 相同,但使用
r1
作为第一个源范围,r2
作为第二个源范围,就像使用ranges::begin(r1)
作为first1
,ranges::end(r1)
作为last1
,ranges::begin(r2)
作为first2
,ranges::end(r2)
作为last2
。
本页描述的函数类实体是niebloids。
参数
first1 last1 | 要检查的元素范围。 |
r1 | 要检查的元素范围。 |
first2 last2 | 用作后缀的元素范围。 |
r2 | 用作后缀的元素范围。 |
pred | 用于比较元素的二元谓词。 |
proj1 | 应用于第一个范围中元素的投影。 |
proj2 | 应用于第二个范围中元素的投影。 |
返回值
如果第二个范围与第一个范围的前缀匹配,则为 true
,否则为 false
。
更具体地说
设 N1
和 N2
分别表示范围 [first1
; last1
) 和 [first2
; last2
) 的大小。
- 如果
N1 < N2
,返回false
。 - 否则,如果范围 [
first2
;last2
) 中的每个元素都等于 [first1 + N1 - N2
;last1
) 中的相应元素(在投影后使用pred
比较),则返回true
。
复杂度
谓词和两个投影最多应用 min(N1, N2)
次。如果 N1 < N2
,则不应用谓词和两个投影
如果 N1
和 N2
都可以用常数时间计算(即,迭代器-哨兵类型对都建模 sized_sentinel_for
,或者两个范围类型都建模 sized_range
),并且 N1 < N2
,则时间复杂度是 常数。
异常
(无)
可能的实现
ends_with(1) 和 starts_with(2)
struct ends_with_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::forward_iterator<I1> || std::sized_sentinel_for<S1, I1>) &&
(std::forward_iterator<I2> || std::sized_sentinel_for<S2, I2>) &&
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
{
const auto n1 = ranges::distance(first1, last1);
const auto n2 = ranges::distance(first2, last2);
if (n1 < n2)
return false;
ranges::advance(first1, n1 - n2);
return ranges::equal(std::move(first1), std::move(last1),
std::move(first2), std::move(last2),
std::move(pred), std::move(proj1), std::move(proj2));
}
template<ranges::input_range R1, ranges::input_range R2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity>
requires (ranges::forward_range<R1> || ranges::sized_range<R1>) &&
(ranges::forward_range<R2> || ranges::sized_range<R2>) &&
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::move(pred), std::move(proj1), std::move(proj2));
}
};
inline constexpr ends_with_fn ends_with {};
示例
#include <algorithm>
#include <array>
#include <iostream>
int main()
{
std::cout
<< std::boolalpha
<< std::ranges::ends_with("static_cast", "cast") << '\n'
<< std::ranges::ends_with("const_cast", "cast") << '\n'
<< std::ranges::ends_with("reinterpret_cast", "cast") << '\n'
<< std::ranges::ends_with("dynamic_cast", "cast") << '\n'
<< std::ranges::ends_with("move", "cast") << '\n'
<< std::ranges::ends_with("move_if_noexcept", "cast") << '\n'
<< std::ranges::ends_with("forward", "cast") << '\n';
static_assert(
! std::ranges::ends_with("as_const", "cast") and
!! std::ranges::ends_with("bit_cast", "cast") and
! std::ranges::ends_with("to_underlying", "cast") and
!! std::ranges::ends_with(std::array {1,2,3,4}, std::array {3,4}) and
! std::ranges::ends_with(std::array {1,2,3,4}, std::array {4,5})
);
}
true
true
true
true
false
false
false