std::ranges::subrange
From cppreference.com
Defined in header <ranges>
|
||
template< std::input_or_output_iterator I, |
(since C++20) | |
The subrange
class template combines together an iterator and a sentinel into a single
view
.
Additionally, the subrange is a
sized_range
whenever the final template parameter is subrange_kind::sized (which happens when std::sized_sentinel_for<S, I> is satisfied or when size is passed explicitly as a constructor argument).
Contents |
[edit] Member constants
Constant name | Definition |
bool StoreSize (private) [static]
|
K == ranges::subrange_kind::sized && !std::sized_sentinel_for<S, I> (exposition-only static member constant*) |
[edit] Data members
Member name | Definition |
begin_ (private)
|
an iterator of type I (exposition-only member object*) |
end_ (private)
|
a sentinel of type S (exposition-only member object*) |
size_ (private) (conditionally present) |
the subrange's size value of type /*make-unsigned-like-t*/<std::iter_difference_t<I>>; present only if StoreSize is true.(exposition-only member object*) |
[edit] Member functions
creates a new subrange (public member function) | |
converts the subrange to a pair-like type (public member function) | |
Observers | |
obtains the iterator (public member function) | |
obtains the sentinel (public member function) | |
checks whether the subrange is empty (public member function) | |
obtains the size of the subrange (public member function) | |
Iterator operations | |
advances the iterator by given distance (public member function) | |
obtains a copy of the subrange with its iterator decremented by a given distance (public member function) | |
obtains a copy of the subrange with its iterator advanced by a given distance (public member function) | |
Inherited from std::ranges::view_interface | |
(C++23) |
returns a constant iterator to the beginning of the range. (public member function of std::ranges::view_interface<D> )
|
(C++23) |
returns a sentinel for the constant iterator of the range. (public member function of std::ranges::view_interface<D> )
|
returns whether the derived view is not empty. Provided if ranges::empty is applicable to it. (public member function of std::ranges::view_interface<D> )
| |
gets the address of derived view's data. Provided if its iterator type satisfies
contiguous_iterator . (public member function of std::ranges::view_interface<D> )
| |
returns the first element in the derived view. Provided if it satisfies
forward_range . (public member function of std::ranges::view_interface<D> )
| |
returns the last element in the derived view. Provided if it satisfies
bidirectional_range and
common_range . (public member function of std::ranges::view_interface<D> )
| |
returns the n th element in the derived view. Provided if it satisfies
random_access_range . (public member function of std::ranges::view_interface<D> )
|
[edit] Deduction guides
[edit] Non-member functions
(C++20) |
obtains iterator or sentinel from a std::ranges::subrange (function template) |
[edit] Helper types
(C++20) |
specifies whether a std::ranges::subrange models std::ranges::sized_range (enum) |
obtains the number of components of a std::ranges::subrange (class template specialization) | |
obtains the type of the iterator or the sentinel of a std::ranges::subrange (class template specialization) |
[edit] Helper templates
template< class I, class S, ranges::subrange_kind K > constexpr bool ranges::enable_borrowed_range<ranges::subrange<I, S, K>> = true; |
(since C++20) | |
This specialization of ranges::enable_borrowed_range makes subrange
satisfy
borrowed_range
.
[edit] Example
Run this code
#include <map> #include <print> #include <ranges> void make_uppercase(char& v) { v += 'A' - 'a'; } void uppercase_transform(std::multimap<int, char>& m, int k) { auto [first, last] = m.equal_range(k); for (auto& [_, v] : std::ranges::subrange(first, last)) make_uppercase(v); } int main() { std::multimap<int, char> mm{{4, 'a'}, {3, '-'}, {4, 'b'}, {5, '-'}, {4, 'c'}}; std::println("Before: {}", mm); uppercase_transform(mm, 4); std::println("After: {}", mm); }
Output:
Before: {3: '-', 4: 'a', 4: 'b', 4: 'c', 5: '-'} After: {3: '-', 4: 'A', 4: 'B', 4: 'C', 5: '-'}
[edit] See also
(C++20) |
helper class template for defining a
view , using the curiously recurring template pattern (class template) |
[edit] External links
Read/write all values of a std::multimap with a given key in C++20 — SO
|