Namespaces
Variants

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

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
Definido en el encabezado <numeric>
Firma de llamada
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 ) ;
(1) (desde 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 ) ;
(2) (desde C++23)
Tipos auxiliares
template < class O, class T >
using iota_result = ranges:: out_value_result < O, T > ;
(3) (desde C++23)

Llena el rango [ first , last ) con valores secuencialmente crecientes, comenzando con value y evaluando repetidamente ++ value .

Operación equivalente:

*(first)     = value;
*(first + 1) = ++value;
*(first + 2) = ++value;
*(first + 3) = ++value;
...

Contenidos

Parámetros

first, last - el par iterador-sentencia que define el rango de elementos a llenar con valores secuencialmente crecientes comenzando con value
value - valor inicial a almacenar; la expresión ++ value debe estar bien formada

Valor de retorno

{ último, valor + ranges:: distance ( primero, último ) }

Complejidad

Exactamente last - first incrementos y asignaciones.

Implementación posible

struct iota_fn
{
    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> operator()(O first, S last, T value) const
    {
        while (first != last)
        {
            *first = as_const(value);
            ++first;
            ++value;
        }
        return {std::move(first), std::move(value)};
    }
    template<std::weakly_incrementable T, std::ranges::output_range<const T&> R>
    constexpr iota_result<std::ranges::borrowed_iterator_t<R>, T>
    operator()(R&& r, T value) const
    {
        return (*this)(std::ranges::begin(r), std::ranges::end(r), std::move(value));
    }
};
inline constexpr iota_fn iota;

Notas

La función recibe su nombre de la función entera del lenguaje de programación APL .

Macro de prueba de características Valor Std Característica
__cpp_lib_ranges_iota 202202L (C++23) std::ranges::iota

Ejemplo

Utiliza el vector de iteradores ( std:: vector < std:: list < T > :: iterator > ) como un proxy para barajar los elementos de la std::list , porque ranges::shuffle no puede aplicarse directamente a la std::list .

#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; });
}

Salida posible:

List: 0 1 2 3 4 5 6 7
List viewed via vector: 5 7 6 0 1 3 4 2

Véase también

asigna por copia el valor dado a cada elemento en un rango
(plantilla de función)
asigna un valor determinado a un rango de elementos
(objeto función de algoritmo)
asigna los resultados de llamadas sucesivas de función a cada elemento en un rango
(plantilla de función)
guarda el resultado de una función en un rango
(objeto función de algoritmo)
una view que consiste en una secuencia generada incrementando repetidamente un valor inicial
(plantilla de clase) (objeto punto de personalización)
(C++11)
llena un rango con incrementos sucesivos del valor inicial
(plantilla de función)