std::ranges...s::iota_view(3)
π¦ std::ranges::views::iota, std::ranges::iota_view β C++ Standard Libary π¦
okay bestie, this is the manual page for std::ranges...s::iota_view(3) from the C++ Standard Libary, and it is genuinely iconic. itβs giving βmake me a range by justβ¦ incrementing forever,β and honestly? slay.
π NAME π
std::ranges::views::iota, std::ranges::iota_view β the range factory duo that keeps on incrementing, no cap.
β¨ Synopsis β¨
Defined in header <ranges> π«Ά
template< std::weakly_incrementable W,
std::semiregular Bound = std::unreachable_sentinel_t >
requires __WeaklyEqualityComparableWith<W, Bound> &&
std::copyable<W>
class iota_view
: public ranges::view_interface<iota_view<W, Bound>>
Thatβs (1) β (since C++20) π
namespace views {
inline constexpr /* unspecified */ iota = /* unspecified */;
}
Thatβs (2) β (since C++20) β¨
π Call signature π
template< class W >
requires /* see below */
constexpr /* see below */ iota( W&& value );
(since C++20)
template< class W, class Bound >
requires /* see below */
constexpr /* see below */ iota( W&& value, Bound&& bound );
(since C++20)
- A range factory that generates a sequence of elements by repeatedly incrementing an initial value. Can be either bounded or unbounded (infinite). π₯
views::iota(e)andviews::iota(e, f)are expression-equivalent toiota_view(e)andiota_view(e, f)respectively for any suitable subexpressionseandf. periodt.
πΈ Member functions πΈ
constructor(C++20)β creates aniota_view(public member function)begin(C++20)β obtains the beginning iterator of aniota_view(public member function)end(C++20)β obtains the sentinel denoting the end of aniota_view(public member function)empty(C++20)β tests whether theiota_viewis empty, i.e. the iterator and the sentinel compare equal (public member function)size(C++20)β obtains the size of aniota_viewif it is sized (public member function)
π Inherited from std::ranges::view_interface π
cbegin(C++23)β returns a constant iterator to the beginning of the range. (public member function ofstd::ranges::view_interface<D>)cend(C++23)β returns a sentinel for the constant iterator of the range. (public member function ofstd::ranges::view_interface<D>)operator bool(C++20)β returns whether the derived view is not empty. Provided ifranges::emptyis applicable to it. (public member function ofstd::ranges::view_interface<D>)front(C++20)β returns the first element in the derived view. Provided if it satisfiesforward_range. (public member function ofstd::ranges::view_interface<D>)back(C++20)β returns the last element in the derived view. Provided if it satisfiesbidirectional_rangeandcommon_range. (public member function ofstd::ranges::view_interface<D>)operator[](C++20)β returns then^th element in the derived view. Provided if it satisfiesrandom_access_range. (public member function ofstd::ranges::view_interface<D>)
π§ std::ranges::iota_view::iota_view π§
iota_view() requires std::default_initializable<W> = default;
(1) (since C++20)
constexpr explicit iota_view( W value );
(2) (since C++20)
constexpr explicit iota_view( std::type_identity_t<W> value,
std::type_identity_t<Bound> bound );
(3) (since C++20)
constexpr explicit iota_view( /* iterator */ first, /* see below */ last );
(4) (since C++20)
- Value-initializes
value_andbound_via their default member initializers (= W()and= Bound()). π - Initializes
value_withvalueand value-initializesbound_. This constructor is used to create unboundediota_views, e.g.iota(0)yields numbers0,1,2...,infinitely. π - Initializes
value_withvalueandbound_withbound. The behavior is undefined ifstd::totally_ordered_with<W, Bound>is modeled andbool(value <= bound)is false. This constructor is used to create bounded iota views, e.g.iota(10, 20)yields numbers from10to19. π - Same as (3), except that
value_is initialized with theWvalue stored infirst, and- if
WandBoundare the same type, then the type oflastis/* iterator */andbound_initialized with theWvalue stored inlast, - otherwise, if the
iota_viewis unbounded (i.e.Boundisstd::unreachable_sentinel_t), then the type oflastisstd::unreachable_sentinel_tandbound_initialized withstd::unreachable_sentinel. - otherwise, the type of
lastis/* sentinel */andbound_initialized with theBoundvalue stored inlast. In any case, the type oflastis same asdecltype(end()). π«
- if
For (2), (3), and (4), the behavior is undefined if the iota_view is bounded (i.e. Bound is not std::unreachable_sentinel_t) and bound_ is initialized to a value unreachable from value_.
β οΈ bestie, that last sentence is real: unreachable bound_ means undefined behavior. do not test it for fun. π
π Parameters π
valueβ the starting valueboundβ the boundfirstβ the iterator denoting the starting valuelastβ the iterator or sentinel denoting the bound
π std::ranges::iota_view::begin π
constexpr /* iterator */ begin() const;
(since C++20)
Returns an iterator initialized with value_. π«Ά
π std::ranges::iota_view::end π
constexpr auto end() const;
(1) (since C++20)
constexpr /* iterator */ end() const requires std::same_as<W, Bound>;
(2) (since C++20)
- Returns a sentinel of a specific type (shown as
/* sentinel */here) initialized withbound_if this view is bounded, orstd::unreachable_sentinelif this view is unbounded. β¨ - Returns an iterator initialized with
bound_. π
π¦ std::ranges::iota_view::empty π¦
constexpr bool empty() const;
(since C++20)
Equivalent to return value_ == bound_;. simple, clean, iconic.
π std::ranges::iota_view::size π
constexpr auto size() const
requires (std::same_as<W, Bound> && /* advanceable */<W>)
|| (/* is-integer-like */<W> && /* is-integer-like */<Bound>)
|| std::sized_sentinel_for<Bound, W>
{
if constexpr (/* is-integer-like */<W> && /* is-integer-like */<Bound>)
return (value_ < 0)
? ((bound_ < 0)
? /* to-unsigned-like */(-value_)
- /* to-unsigned-like */(-bound_)
: /* to-unsigned-like */(bound_)
+ /* to-unsigned-like */(-value_))
: /* to-unsigned-like */(bound_) - /* to-unsigned-like */(value_);
else
return /* to-unsigned-like */(bound_ - value_);
}
(since C++20)
Returns the size of the view if the view is bounded. π₯
The exposition-only concept advanceable is described in this page.
The exposition-only function template to-unsigned-like converts its argument (which must be integer-like) to the corresponding unsigned version of the argument type.
π Deduction guides π
template< class W, class Bound >
requires (!/* is-integer-like */<W>
|| !/* is-integer-like */<Bound>
|| /* is-signed-integer-like */<W>
== /* is-signed-integer-like */<Bound>)
iota_view( W, Bound ) -> iota_view<W, Bound>;
(since C++20)
For any type T, /* is-integer-like */<T> is true if and only if T is integer-like, and /* is-signed-integer-like */<T> is true if and only if T is integer-like and capable of representing negative values.
Note that the guide protects itself against signed/unsigned mismatch bugs, like views::iota(0, v.size()), where 0 is a (signed) int and v.size() is an (unsigned) std::size_t. π
π§ Nested classes π§
iterator(C++20)β the iterator type (exposition-only member class*)sentinel(C++20)β the sentinel type used when theiota_viewis bounded andBoundandWare not the same type (exposition-only member class*)
π Helper templates π
template< std::weakly_incrementable W, std::semiregular Bound >
inline constexpr bool enable_borrowed_range<ranges::iota_view<W, Bound>> = true;
(since C++20)
This specialization of std::ranges::enable_borrowed_range makes iota_view satisfy borrowed_range. π
πΈ Example πΈ
// Run this code
#include <algorithm>
#include <iostream>
#include <ranges>
struct Bound
{
int bound;
bool operator==(int x) const { return x == bound; }
};
int main()
{
for (int i : std::ranges::iota_view{1, 10})
std::cout << i << ' ';
std::cout << '\n';
for (int i : std::views::iota(1, 10))
std::cout << i << ' ';
std::cout << '\n';
for (int i : std::views::iota(1, Bound{10}))
std::cout << i << ' ';
std::cout << '\n';
for (int i : std::views::iota(1) | std::views::take(9))
std::cout << i << ' ';
std::cout << '\n';
std::ranges::for_each(std::views::iota(1, 10), [](int i)
{
std::cout << i << ' ';
});
std::cout << '\n';
}
β Output β
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 3523β Applied to:C++20. Behavior as published: iterator-sentinel pair constructor might use wrong sentinel type. Correct behavior: corrected.LWG 3610β Applied to:C++20. Behavior as published:sizemight reject integer-class types. Correct behavior: accept if possible.LWG 4001β Applied to:C++20. Behavior as published: the inherited memberemptyfunction was not always valid. Correct behavior:emptyis always provided.P2325R3β Applied to:C++20. Behavior as published:iota_viewrequired thatWissemiregularas view requireddefault_initializable. Correct behavior: only requires thatWiscopyable.P2711R1β Applied to:C++20. Behavior as published: the multi-parameter constructors were not explicit. Correct behavior: made explicit.
π See also π
okay bestie, here's the little βgo look at these tooβ corner β lowkey useful, ngl:
iotaβ fills a range with successive increments of the starting value Β· (C++11) Β· (function template)ranges::iotaβ fills a range with successive increments of the starting value Β· (C++23) Β· (niebloid)ranges::repeat_viewβ a view consisting of a generated sequence by repeatedly producing the same value Β· (C++23) Β· (class template)views::repeatβ same βa view consisting of a generated sequence by repeatedly producing the same valueβ energy Β· (C++23) Β· (customization point object)
thatβs the whole see-also situation, bestie π«Ά source/version + date: http://cppreference.com Β· 2024.06.10 Β· page: std::ranges...s::iota_view(3) π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms