Namespaces
Variants

std::ranges::view_interface<D>::size

从 zh.cppreference.net
 
 
范围库
范围适配器
 
 
constexpr auto size() requires ranges::forward_range<D> &&
    std::sized_sentinel_for<ranges::sentinel_t<D>,
                            ranges::iterator_t<D>>;
(1) (自 C++20)
constexpr auto size() const requires ranges::forward_range<const D> &&
    std::sized_sentinel_for<ranges::sentinel_t<const D>,
                            ranges::iterator_t<const D>>;
(2) (自 C++20)

成员函数 size() size 的默认实现通过计算哨位与起始迭代器之间的差值来获取范围的大小。

返回值

1) to-unsigned-like  ( ranges:: end ( static_cast < D & > ( this ) ) -
ranges:: begin ( static_cast < D & > ( this ) ) )
.
2) to-unsigned-like  ( ranges:: end ( static_cast < const D & > ( this ) ) -
ranges:: begin ( static_cast < const D & > ( this ) ) )
.
(说明:根据要求,HTML标签、属性及 标签内的C++代码均未翻译,仅对数字序号"2)"后的描述性文本进行了本地化处理,将英文说明转换为简体中文表述)

注释

以下派生类型可以使用 size() 的默认实现:

以下类型派生自 std::ranges::view_interface 且未声明自身的 size() 成员函数,但它们无法使用默认实现,因为其迭代器和哨位类型始终不满足 sized_sentinel_for :

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 3646 C++20 size 函数的默认实现返回有符号类型 返回无符号类型

参见

(C++17) (C++20)
返回容器或数组的大小
(函数模板)
返回等于范围大小的整数
(定制点对象)
返回等于范围大小的有符号整数
(定制点对象)