std::ranges::enumerate_view<V>::size
来自cppreference.com
| |
(1) | (C++23 起) |
| |
(2) | (C++23 起) |
返回元素数量。等价于 return ranges::size(base_);,其中 base_ 表示底层视图。
参数
(无)
返回值
元素数量。
示例
运行此代码
#include <cassert>
#include <forward_list>
#include <ranges>
#include <string_view>
int main()
{
constexpr static auto v1 = {1, 2, 3, 4, 5};
constexpr auto ev1{v1 | std::views::enumerate};
static_assert(ev1.size() == 5);
static_assert(std::ranges::sized_range<decltype(v1)>);
auto v2 = std::forward_list{1, 2, 3, 4, 5};
auto ev2 {v2 | std::views::enumerate};
static_assert(not std::ranges::sized_range<decltype(v2)>)
// auto size = ev2.size(); // 错误: v2 不是有大小范围
assert(std::ranges::distance(v2) == 5); // OK, distance 不要求有大小范围
// 但此处复杂度是 O(N)
}
参阅
(C++20) |
返回等于范围大小的整数 (定制点对象) |
(C++20) |
返回等于范围大小的有符号整数 (定制点对象) |