std::ranges::subrange<I,S,K>::size
來自 cppreference.com
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 是否為空(public member function) | |
(C++17)(C++20) |
返回容器或陣列的大小 (function template) |
(C++20) |
返回等於範圍大小的整數 (定製點物件) |