A lazy range of integers that mimics a Python range.
It stores the first value, the last value (excluded) and the step size between two indices. By default, the step size is set to 1.
This function returns an iterable lazy object, which can be used in range-based for loops:
for (
auto i :
range(5)) {
std::cout << i << " ";
}
std::cout << "\n";
for (
auto i :
range(-2, 1)) {
std::cout << i << " ";
}
std::cout << "\n";
for (
auto i :
range(10, 3, -2)) {
std::cout << i << " ";
}
std::cout << "\n";
for (
auto i :
range(0, 10, -1)) {
std::cout << i << " ";
}
Output:
0 1 2 3 4
-2 -1 0
10 8 6 4
See also std::ranges::views::iota.
Definition at line 83 of file range.hpp.
|
| range ()=default |
| Default constructor.
|
|
| range (std::integral auto first, std::integral auto last) noexcept |
| Construct a range with a step size of 1 and a given first and last (excluded) value.
|
|
| range (std::integral auto first, std::integral auto last, std::integral auto step) |
| Construct a range with a given step size and a given first and last (excluded) value.
|
|
| range (std::integral auto last) |
| Construct a range with a step size of 1, a first value set to 0 and a given last value (excluded).
|
|
const_iterator | begin () const noexcept |
| The same as cbegin().
|
|
const_iterator | cbegin () const noexcept |
| Beginning of the integer range.
|
|
const_iterator | cend () const noexcept |
| End of the range.
|
|
const_reverse_iterator | crbegin () const noexcept |
| Beginning of the integer range in reverse order.
|
|
const_reverse_iterator | crend () const noexcept |
| End of the range in reverse order.
|
|
const_iterator | end () const noexcept |
| The same as cend().
|
|
long | first () const |
| Get first value of the range.
|
|
long | last () const |
| Get last value of the range (excluded).
|
|
range | operator+ (long shift) const |
| Shift the whole range by a given amount.
|
|
bool | operator== (range const &) const =default |
| Default equal-to operator.
|
|
const_reverse_iterator | rbegin () const noexcept |
| The same as crbegin().
|
|
const_reverse_iterator | rend () const noexcept |
| The same as crend().
|
|
long | size () const |
| Get number of elements in the range.
|
|
long | step () const |
| Get step size between two elements of the range.
|
|