std::ranges::slide_view<V>::size
来自 cppreference.com
< cpp | ranges | slide view
constexpr auto size() requires ranges::sized_range<V>; |
(1) | (自 C++23) |
constexpr auto size() const requires ranges::sized_range<const V>; |
(2) | (自 C++23) |
返回元素数量。
等效于
auto sz = ranges::distance(base_) - n_ + 1; if (sz < 0) sz = 0; return /*to-unsigned-like*/(sz);
内容 |
[编辑] 参数
(无)
[编辑] 返回值
元素的数量。如果底层视图 base_
中的元素数量 (ranges::size(base_)) 小于“窗口大小” n_
,则等于 0。
[编辑] 示例
运行此代码
#include <forward_list> #include <iostream> #include <list> #include <ranges> int main() { constexpr static auto v = {1, 2, 3, 4, 5, 6}; constexpr int width1{4}; constexpr auto view1{std::views::slide(v, width1)}; static_assert(view1.size() == 3); static_assert(view1.size() == (v.size() - width1 + 1)); constexpr int width2{8}; constexpr auto view2{std::views::slide(v, width2)}; // window is too wide, so view2 has no elements: static_assert(view2.size() == 0); std::forward_list forward_list = v; const auto view3{std::views::slide(forward_list, width1)}; // auto x = view3.size(); // error: sized_range constraint is not satisfied std::list list = v; const auto view4{std::views::slide(list, width1)}; std::cout << view4.size() << '\n'; // prints 3 }
输出
3
[编辑] 另请参阅
(C++20) |
返回一个等于范围大小的整数 (定制点对象) |
(C++20) |
返回一个等于范围大小的有符号整数 (定制点对象) |