On this page
std::ranges::subrange
| Defined in header <ranges> | ||
|---|---|---|
|  | (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.
Member functions
| (C++20)
        | creates a new subrange(public member function) | 
| (C++20)
        | converts the subrangeto apair-liketype(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 subrangeis empty(public member function) | 
| (C++20)
        | obtains the size of the subrange(public member function) | 
| Iterator operations | |
| (C++20)
        | advances the iterator by given distance (public member function) | 
| (C++20)
        | obtains a copy of the subrangewith its iterator decremented by a given distance(public member function) | 
| (C++20)
        | obtains a copy of the subrangewith its iterator advanced by a given distance(public member function) | 
| Inherited from  | |
| (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>) | 
| (C++20)
        | returns whether the derived view is not empty. Provided if ranges::emptyis applicable to it.(public member function of std::ranges::view_interface<D>) | 
| (C++20)
        | 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>) | 
| (C++20)
        | returns the first element in the derived view. Provided if it satisfies forward_range.(public member function of std::ranges::view_interface<D>) | 
| (C++20)
        | returns the last element in the derived view. Provided if it satisfies bidirectional_rangeandcommon_range.(public member function of std::ranges::view_interface<D>) | 
| (C++20)
        | returns the nth element in the derived view. Provided if it satisfies random_access_range.(public member function of std::ranges::view_interface<D>) | 
Deduction guides
Non-member functions
| (C++20)
        | obtains iterator or sentinel from a std::ranges::subrange(function template) | 
Helper types
| (C++20)
        | specifies whether a std::ranges::subrangemodelsstd::ranges::sized_range(enum) | 
| (C++20)
        | obtains the number of components of a std::ranges::subrange(class template specialization) | 
| (C++20)
        | obtains the type of the iterator or the sentinel of a std::ranges::subrange(class template specialization) | 
Helper templates
|  | 
This specialization of std::ranges::enable_borrowed_range makes subrange satisfy borrowed_range.
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,'-'} }See also
| (C++20)
        | helper class template for defining a view, using the curiously recurring template pattern(class template) | 
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
 https://en.cppreference.com/w/cpp/ranges/subrange