std::ranges::ends_with
定义于头文件 <algorithm> |
||
调用签名 |
||
template< std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, |
(1) | (since C++23) |
template< ranges::input_range R1, ranges::input_range R2, class Pred = ranges::equal_to, |
(2) | (since C++23) |
检查第二个范围是否匹配第一个范围的后缀。
- 如果 N1 < N2 为 true,返回 false。
- 否则,返回 ranges::equal(std::move(first1) + (N1 - N2), last1,
std::move(first2), last2, pred, proj1, proj2)。
- 如果 N1 < N2 为 true,返回 false。
- 否则,返回 ranges::equal(views::drop(ranges::ref_view(r1),
N1 - static_cast<decltype(N1)>(N2)),
r2, pred, proj1, proj2)。
此页面上描述的类函数实体是算法函数对象 (非正式地称为niebloids),也就是
目录 |
[编辑] 参数
first1, last1 | - | 定义要检查的元素的范围的迭代器-哨位对 |
r1 | - | 要检查的元素的范围 |
first2, last2 | - | 定义要用作后缀的元素的范围的迭代器-哨位对 |
r2 | - | 要用作后缀的元素的范围 |
pred | - | 比较投影元素的二元谓词 |
proj1 | - | 要应用于要检查的范围的元素的投影 |
proj2 | - | 要应用于要用作后缀的范围的元素的投影 |
[编辑] 返回值
true 如果第二个范围匹配第一个范围的后缀,否则为 false。
[编辑] 复杂度
通常为线性:至多 min(N1,N2) 次谓词和两个投影的应用。如果 N1 < N2 为 true,则不应用谓词和两个投影。
如果 N1 和 N2 都可以在常数时间内计算出来(即,两个迭代器-哨位类型对都对 sized_sentinel_for
建模,或者两个范围类型都对 sized_range
建模)并且 N1 < N2 为 true,则时间复杂度为常数。
[编辑] 可能的实现
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), last1, std::move(first2), last2, pred, proj1, 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 { const auto n1 = ranges::distance(r1); const auto n2 = ranges::distance(r2); if (n1 < n2) return false; return ranges::equal(views::drop(ranges::ref_view(r1), n1 - static_cast<decltype(n1)>(n2)), r2, pred, proj1, proj2); } }; inline constexpr ends_with_fn ends_with{}; |
[编辑] 注解
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_ranges_starts_ends_with |
202106L |
(C++23) | std::ranges::starts_with, std::ranges::ends_with |
[编辑] 示例
#include <algorithm> #include <array> static_assert ( ! std::ranges::ends_with("for", "cast") && std::ranges::ends_with("dynamic_cast", "cast") && ! std::ranges::ends_with("as_const", "cast") && std::ranges::ends_with("bit_cast", "cast") && ! std::ranges::ends_with("to_underlying", "cast") && std::ranges::ends_with(std::array{1, 2, 3, 4}, std::array{3, 4}) && ! std::ranges::ends_with(std::array{1, 2, 3, 4}, std::array{4, 5}) ); int main() {}
[编辑] 缺陷报告
以下行为更改缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 4105 | C++23 | 重载 (2) 计算了大小 差异通过 N1 - N2[1] |
更改为 N1 - static_cast<decltype(N1)>(N2) |
[编辑] 参见
(C++23) |
检查一个范围是否以另一个范围开始 (算法函数对象) |
(C++20) |
检查字符串是否以给定后缀结尾 ( std::basic_string<CharT,Traits,Allocator> 的公共成员函数) |
(C++20) |
检查字符串视图是否以给定后缀结尾 ( std::basic_string_view<CharT,Traits> 的公共成员函数) |