std::ranges::iota, std::ranges::iota_result

Header: <numeric>

Fills the range [first,last) with sequentially increasing values, starting with value and repetitively evaluating ++value.

# Declarations

Call signature
template< std::input_or_output_iterator O, std::sentinel_for<O> S,
std::weakly_incrementable T >
requires std::indirectly_writable<O, const T&>
constexpr iota_result<O, T>
iota( O first, S last, T value );

(since C++23)

template< std::weakly_incrementable T, ranges::output_range<const T&> R >
constexpr iota_result<ranges::borrowed_iterator_t<R>, T>
iota( R&& r, T value );

(since C++23)

Helper types
template< class O, class T >
using iota_result = ranges::out_value_result<O, T>;

(since C++23)

# Parameters

# Return value

{last, value + ranges::distance(first, last)}

# Notes

The function is named after the integer function ⍳ from the programming language APL.

# Example

#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <random>
#include <vector>
 
template <typename Proj = std::identity>
void println(auto comment, std::ranges::input_range auto&& range, Proj proj = {})
{
    for (std::cout << comment; auto const &element : range)
        std::cout << proj(element) << ' ';
    std::cout << '\n';
}
 
int main()
{
    std::list<int> list(8);
 
    // Fill the list with ascending values: 0, 1, 2, ..., 7
    std::ranges::iota(list, 0);
    println("List: ", list);
 
    // A vector of iterators (see the comment to Example)
    std::vector<std::list<int>::iterator> vec(list.size());
 
    // Fill with iterators to consecutive list's elements
    std::ranges::iota(vec.begin(), vec.end(), list.begin());
 
    std::ranges::shuffle(vec, std::mt19937 {std::random_device {}()});
    println("List viewed via vector: ", vec, [](auto it) { return *it; });
}

# See also