命名空间
变体
操作

std::ranges::subrange<I,S,K>::size

来自 cppreference.cn
< cpp‎ | ranges‎ | subrange
 
 
范围库
范围适配器
 
 
constexpr /*make-unsigned-like-t*/<std::iter_difference_t<I>> size() const
    requires (K == ranges::subrange_kind::sized);
(C++20 起)

获取 subrange 中的元素数量

  • 如果 StoreSize 为 true,则返回 size_。
  • 否则,返回 to-unsigned-like(end_ - begin_)。

有关 /*make-unsigned-like-t*/ 的定义,请参阅 make-unsigned-like-t。

[编辑] 返回值

如上所述。

[编辑] 示例

#include <functional>
#include <iostream>
#include <ranges>
#include <utility>
 
int main()
{
    const auto v = {2, 2, 2, 7, 1, 1, 1, 1, 8, 2, 2, 2, 2, 2};
 
    // the value type of views::chunk_by is the ranges::subrange
 
    auto to_pair = [](auto sub) { return std::make_pair(sub[0], sub.size()); };
                                                                 /* ^^^^ */
    auto pairs = v | std::views::chunk_by(std::equal_to{})
                   | std::views::transform(to_pair);
 
    for (auto x : pairs bitor std::views::keys)
        std::cout << x << ' ';
    std::cout << '\n';
    for (auto x : pairs bitor std::views::values)
        std::cout << x << ' ';
    std::cout << '\n';
}

输出

2 7 1 8 2
3 1 4 1 5

[编辑] 参见

检查 subrange 是否为空
(公共成员函数) [编辑]
(C++17)(C++20)
返回容器或数组的大小
(函数模板) [编辑]
返回等于范围大小的整数
(定制点对象)[编辑]