std::ranges::subrange<I,S,K>::size
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Ranges library
(C++23)(C++23)
(C++26)(C++26)
(C++23)(C++23)
(C++26)(C++26)
(C++26)(C++26)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)
(C++23)(C++23)
(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
std::ranges::subrange
subrange::size
constexpr /*make-unsigned-like-t*/<std::iter_difference_t <I>> size() const
requires (K == ranges::subrange_kind::sized);
(since C++20)
requires (K == ranges::subrange_kind::sized);
Obtains the number of elements in the subrange
:
- If
StoreSize
is true, returnssize_
. - Otherwise, returns
to-unsigned-like
(end_
begin_
).
For the definition of /*make-unsigned-like-t*/, see make-unsigned-like-t
.
[edit] Return value
As described above.
[edit] Example
Run this code
#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'; }
Output:
2 7 1 8 2 3 1 4 1 5