Namespaces
Variants

std::ranges::stride_view<V>:: 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_ 为底层视图, stride_ 为存储的步长值。等价于:

return /*转换为无符号类型*/(/*向上取整除法*/(ranges::distance(base_), stride_));

目录

参数

(无)

返回值

元素数量。返回值计算方式相当于表达式

( ranges:: size ( base_ ) / stride_ ) + ( ( ranges:: size ( base_ ) % stride_ ? 1 : 0 )

示例

#include <forward_list>
#include <ranges>
int main()
{
    namespace vs = std::views;
    constexpr static auto v = {1, 2, 3, 4, 5};
    static_assert
    (
        vs::stride(v, 1).size() == 5 and
        vs::stride(v, 2).size() == 3 and
        vs::stride(v, 3).size() == 2 and
        vs::stride(v, 4).size() == 2 and
        vs::stride(v, 5).size() == 1 and
        vs::stride(v, 6).size() == 1
    );
    std::forward_list list{v};
//  auto s = vs::stride(list, 2).size(); // 错误:不是可大小范围
}

参见

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