template<WeaklyIncrementable W, Semiregular Bound = unreachable_sentinel_t> requires _WeaklyEqualityComparableWith<W, Bound> class iota_view : public view_interface<iota_view<W, Bound>> | (1) | (since C++20) |
namespace view { inline constexpr /*unspecified*/ iota = /*unspecified*/; } | (2) | (since C++20) |
view::iota(E)
and view::iota(E, F)
are expression-equivalent to (has the same effect as) iota_view{E}
and iota_view{E, F}
respectively for any suitable subexpressions E
and F
W value_ = W(); /* exposition-only */ |
the current value.
Bound bound_ = Bound(); /* exposition-only */ |
the bound (defaults to std::unreachable_sentinel_t
).
iota_view() = default; | (1) | |
constexpr explicit iota_view(W value); | (2) | |
constexpr iota_view(std::type_identity_t<W> value, std::type_identity_t<Bound> bound); | (3) |
value_
and bound_
value_
with value
and expects that Bound
is either unreachable_sentinel_t
(the default) or Bound()
is reachable from value. This constructor is used to create unbounded iota views, e.g. iota(0)
yields numbers 0,1,2..., infinitely.value_
with value
and bound_
with bound
. This constructor is used to create bounded iota views, e.g. iota(10, 20)
yields numbers from 10 to 19.value | - | the starting value |
bound | - | the bound |
constexpr iterator begin(); |
Returns the iterator initialized with value_
constexpr sentinel end() const; | (1) | |
constexpr iterator end() const requires Same<W, Bound>; | (2) |
Returns the iterator initialized with bound_
if this view is bounded, or the sentinel initialized with bound_
if this view is unbounded.
constexpr auto size() const requires (Same<W, Bound> && _Advanceable<W>) || (Integral<W> && Integral<Bound>) || SizedSentinel<Bound, W> { return bound_ - value_; } |
Returns the size of the view if the view is bounded.
template<class W, class Bound> requires (!Integral<W> || !Integral<Bound> || is_signed_v<W> == is_signed_v<Bound>) iota_view(W, Bound) -> iota_view<W, Bound>; |
Note that the guide protects itself against signed/unsigned mismatch bugs, like view::iota(0, v.size())
, where 0 is a (signed) int and v.size() is an (unsigned) std::size_t.
template<class W, class Bound> struct iota_view<W, Bound>::iterator /* exposition-only */ |
The return type of iota_view::begin
.
This is a RandomAccessIterator
if W
models _Advanceable, a BidirectionalIterator
if W
models _Decrementable, a ForwardIterator
if W
models Incrementable
, and InputIterator
otherwise.
constexpr explicit iterator(W value); |
Initializes exposition-only data member value_
with value
. This value will be returned by operator* and incremented by operator++
constexpr W operator*() const noexcept(is_nothrow_copy_constructible_v<W>); |
returns the current value, by value (in other words, this is a read-only view).
constexpr iterator& operator++() |
Equivalent to.
++value_; return *this;
constexpr iterator& operator--() requires _Decrementable<W>; |
Equivalent to.
--value_; return *this;
constexpr W operator[](difference_type n) const requires _Advanceable<W>; |
Equivalent to.
return value_ + n;
Other members as expected of an iterator.
template<class W, class Bound> struct iota_view<W, Bound>::sentinel /* exposition-only */ |
The return type of iota_view::end
.
Bound bound_ = Bound();; /* exposition only */ |
Exposition-only data member holding the sentinel (typically either a number, for a bounded iota view, or an instance of std::unreachable_sentinel_t
for an unbounded iota view.
entinel() = default; constexpr explicit sentinel(Bound bound); |
Initializes exposition-only data member bound_
with bound
.
friend constexpr bool operator==(const iterator& x, const sentinel& y); friend constexpr bool operator==(const sentinel& x, const iterator& y); |
Equivalent to: x.value_ == y.bound_;
and return y == x;
respectively.
friend constexpr bool operator!=(const iterator& x, const sentinel& y); friend constexpr bool operator!=(const sentinel& x, const iterator& y); |
Equivalent to return !(x == y);
and return !(y == x);
respectively.
#include <ranges> #include <vector> #include <iostream> int main() { for (int i : std::view::iota{1, 10}) std::cout << i << ' '; std::cout << '\n'; for (int i : std::view::iota(1) | std::view::take(9)) std::cout << i << ' '; }
Output:
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/ranges/iota_view