名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | ranges‎ | subrange
 
 
範圍庫 (Ranges library)
範圍介面卡 (Range adaptors)
 
 
constexpr /*make-unsigned-like-t*/<std::iter_difference_t<I>> size() const
    requires (K == ranges::subrange_kind::sized);
(C++20 起)

獲取subrange中元素的數量。

有關/*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是否為空
(public member function) [編輯]
(C++17)(C++20)
返回容器或陣列的大小
(function template) [編輯]
返回等於範圍大小的整數
(定製點物件)[編輯]