Namespaces
Variants

std::ranges::adjacent_view<V,N>:: size

From cppreference.net
Ranges library
Range adaptors
constexpr auto size ( ) requires ranges:: sized_range < V > ;
(C++23 起)
constexpr auto size ( ) const requires ranges:: sized_range < const V > ;
(C++23 起)

返回元素数量。

base_ 为底层视图。等价于:

using SizeType = decltype(ranges::size(base_));
using CommonType = ranges::common_type_t<SizeType, std::size_t>;
auto size = static_cast<CommonType>(ranges::size(base_));
size -= std::min<CommonType>(size, N - 1);
return static_cast<SizeType>(size);

目录

参数

(无)

返回值

元素数量,若 ranges:: size ( base_ ) 小于 N 则可能为 0

示例

#include <ranges>
int main()
{
    constexpr static auto v = {1, 2, 3, 4, 5, 6};
    constexpr int width1 {4};
    constexpr auto view1 {std::views::adjacent<width1>(v)};
    static_assert(view1.size() == 3);
    static_assert(view1.size() == (v.size() - width1 + 1));
    constexpr int width2 {8};
    constexpr auto view2 {std::views::adjacent<width2>(v)};
    // 窗口过宽,因此 view2 没有元素:
    static_assert(view2.size() == 0);
}

参见

返回等于范围大小的整数值
(定制点对象)
返回等于范围大小的有符号整数值
(定制点对象)