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)}; // 窗口过宽,因此 view2 没有元素: static_assert(view2.size() == 0); std::forward_list forward_list = v; const auto view3{std::views::slide(forward_list, width1)}; // auto x = view3.size(); // 错误:不满足 sized_range 的约束 std::list list = v; const auto view4{std::views::slide(list, width1)}; std::cout << view4.size() << '\n'; // 打印 3 }
输出:
3
参阅
(C++20) |
返回等于范围大小的整数 (定制点对象) |
(C++20) |
返回等于范围大小的有符号整数 (定制点对象) |