Namespaces
Variants
Views
Actions

std::ranges::subrange

From cppreference.com
< cpp‎ | ranges
 
 
 
std::ranges::subrange
Member functions
Observers
Modifiers
Deduction guides
Non-member functions
(C++20)
Helper types
(C++20)
 
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 std::sized_sentinel_for<S, I> is satisfied or when size is passed explicitly as a constructor argument). The size record is needed to be stored if and only if std::sized_sentinel_for<S, I> is false and K is subrange_kind::sized.

Contents

[edit] Member functions

creates a new subrange
(public member function)
converts the subrange to a pair-like type
(public member function)
Observers
(C++20)
obtains the iterator
(public member function)
(C++20)
obtains the sentinel
(public member function)
(C++20)
checks whether the subrange is empty
(public member function)
(C++20)
obtains the size of the subrange
(public member function)
Modifiers
(C++20)
advances the iterator by given distance and returns the original subrange
(public member function)
(C++20)
decrements the iterator by a given distance and returns the original subrange
(public member function)
(C++20)
advances the iterator by given distance
(public member function)

[edit] Deduction guides

[edit] Non-member functions

obtains iterator or sentinel from a std::ranges::subrange
(function template) [edit]

[edit] Helper types

specifies whether a std::ranges::subrange models std::ranges::sized_range
(enum) [edit]
obtains the number of components of a std::ranges::subrange
(class template specialization) [edit]
obtains the type of the iterator or the sentinel of a std::ranges::subrange
(class template specialization) [edit]

[edit] Helper templates

template<class I, class 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 <iostream>
#include <map>
#include <ranges>
#include <string_view>
 
template <class V> void mutate(V& v) { v += 'A' - 'a'; }
 
template <class K, class V>
void mutate_map_values(std::multimap<K, V>& m, K k) {
    auto [first, last] = m.equal_range(k);
    for (auto& [_, v] : std::ranges::subrange(first, last)) {
        mutate(v);
    }
}
 
int main()
{
    auto print = [](std::string_view rem, auto const& mm) {
        std::cout << rem << "{ ";
        for (const auto& [k, v] : mm) std::cout << "{" << k << ",'" << v << "'} ";
        std::cout << "}\n";
    };
 
    std::multimap<int,char> mm{ {4,'a'}, {3,'-'}, {4,'b'}, {5,'-'}, {4,'c'} };
    print("Before: ", mm);
    mutate_map_values(mm, 4);
    print("After:  ", mm);
}

Output:

Before: { {3,'-'} {4,'a'} {4,'b'} {4,'c'} {5,'-'} }
After:  { {3,'-'} {4,'A'} {4,'B'} {4,'C'} {5,'-'} }

[edit] See also

helper class template for defining views, using the curiously recurring template pattern
(class template) [edit]