Namespaces
Variants
Views
Actions

std::ranges::subrange

From cppreference.com
< cpp‎ | ranges
 
 
 
Defined in header <ranges>
template<

  std::input_or_output_iterator I,
  std::sentinel_for<I> S = I,
  ranges::subrange_kind K = std::sized_sentinel_for<S, I> ?
    ranges::subrange_kind::sized : ranges::subrange_kind::unsized
>
  requires (K == ranges::subrange_kind::sized || !std::sized_sentinel_for<S, I>)

class subrange : public ranges::view_interface<subrange<I, S, K>>
(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 sized_sentinel_for<S, I> is satisfied or when size is passed explicitly as a constructor argument)

[edit] Helper templates

template<std::input_or_output_iterator I, std::sentinel_for<I> S

         ranges::subrange_kind K>

inline constexpr bool enable_borrowed_range<ranges::subrange<I, S, K>> = true;

This specialization of std::ranges::enable_borrowed_range makes subrange satisfy borrowed_range.

[edit] Example

#include <ranges>
 
void mutate_map_values(std::multimap<K,V>& m, K k) {
    using namespace std::ranges;
    for (auto& [_, v] : subrange(m.equal_range(k))) {
        mutate(v);
    }
}