Namespaces
Variants

std::ranges::concat_view<Views...>:: size

From cppreference.net
Ranges library
Range adaptors
constexpr auto size ( )
requires ( sized_range < Views > && ... ) ;
(1) (C++26 起)
constexpr auto size ( ) const
requires ( sized_range < const Views > && ... ) ;
(2) (C++26 起)

返回元素数量。

等价于 return std:: apply
(
[ ] ( auto ... sizes )
{
using CT = ranges:: common_type_t < decltype ( sizes ) ... > ;
return ( make-unsigned-like-t  < CT > ( sizes ) + ... ) ;
} ,
tuple-transform  ( ranges:: size , views_  )
) ;

目录

返回值

如上所述。

复杂度

常量。

注释

concat_view 的复杂度是常数时间(尽管在某些情况下它是该视图串联的范围数量的线性函数,这是该视图的静态已知参数),因为范围概念所要求的时间复杂度是相对于给定范围的元素总数(大小)正式表述的,而不是相对于该范围的静态已知参数。

示例

初步版本可在 Compiler Explorer 上查看。

#include <cassert>
#include <forward_list>
#include <list>
#include <ranges>
int main()
{
    constexpr static auto a = {1, 2};
    constexpr static auto b = {1, 2, 3};
    constexpr static auto c = {1, 2, 3, 4};
    constexpr auto con{std::views::concat(a, b, c)};
    static_assert(std::ranges::sized_range<decltype(con)>);
    static_assert(con.size() == 2 + 3 + 4);
    std::forward_list d = b;
    static_assert(not std::ranges::sized_range<std::forward_list<int>>);
    const auto cat{std::views::concat(b, c, d)};
    static_assert(not std::ranges::sized_range<decltype(cat)>);
//  auto x = cat.size(); // 错误:由于 d 的存在,cat 不是 sized_range
    std::list e = c;
    const auto dog{std::views::concat(a, b, e)};
    static_assert(std::ranges::sized_range<decltype(dog)>);
    assert(dog.size() == 2 + 3 + 4);
}

参见

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