std::ranges::zip_view<视图...>::size
来自 zh.cppreference.net
constexpr auto size()
requires (ranges::sized_range<Views> && ...);
|
(1) | (自 C++23 起) |
constexpr auto size() const
requires (ranges::sized_range<const Views> && ...);
|
(2) | (自 C++23 起) |
返回 zip_view 中的元素数量。仅在每个底层(适配的)范围满足 sized_range 时提供。
等价于:
return std::apply
(
[](auto... sizes)
{
using CT = /*make-unsigned-like-t*/<std::common_type_t<decltype(sizes)...>>;
return ranges::min({CT(sizes)...});
},
/*tuple-transform*/(ranges::size, views_)
);
参数
(无)
返回值
元素数量,即所有适配的
view
尺寸中的最小值。
示例
运行此代码
#include <algorithm> #include <cassert> #include <deque> #include <forward_list> #include <ranges> #include <vector> int main() { auto x = std::vector{1, 2, 3, 4, 5}; auto y = std::deque{'a', 'b', 'c'}; auto z = std::forward_list{1., 2.}; auto v1 = std::views::zip(x, y); assert(v1.size() == std::min(x.size(), y.size())); assert(v1.size() == 3); [[maybe_unused]] auto v2 = std::views::zip(x, z); // auto sz = v2.size(); // 错误:v2 没有 size() 方法 static_assert(not std::ranges::sized_range<decltype(z)>); }
参见
|
(C++20)
|
返回等于范围大小的整数值
(定制点对象) |
|
(C++20)
|
返回等于范围大小的有符号整数值
(定制点对象) |