Espaces de noms
Variantes
Affichages
Actions

std::iota

De cppreference.com
< cpp‎ | algorithm
 
 
Bibliothèque d'algorithmes
Fonctions
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Non-modification de la séquence des opérations
Original:
Non-modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Modification de la séquence des opérations
Original:
Modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Des opérations de partitionnement
Original:
Partitioning operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Opérations de tri (sur les gammes triés)
Original:
Sorting operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
is_sorted (C++11)
is_sorted_until (C++11)
sort
Opérations binaires de recherche (sur les gammes triés)
Original:
Binary search operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Définir les opérations (sur les gammes triés)
Original:
Set operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Opérations Heap
Original:
Heap operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Minimum / maximum de fonctionnement
Original:
Minimum/maximum operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Opérations numériques
Original:
Numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
iota (C++11)
accumulate
inner_product
Bibliothèque C
Original:
C library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 

Déclaré dans l'en-tête <numeric>
template< class ForwardIt, class T >
void iota( ForwardIt first, ForwardIt last, T value );
(depuis C++11)
template< class ForwardIt, class T >
constexpr void iota( ForwardIt first, ForwardIt last, T value );


Remplis la plage [first, last) avec des valeurs séquenciellement croissantes, commençant par value et répétant ensuite ++value.

Opération équivalente:

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

Sommaire

[modifier] Paramètres

first, last - la plage d'éléments à remplir avec des valeurs séquenciellement croissantes commençant par value
value - valeur initiale à placer; l'expression ++value doit être valide

[modifier] Valeur de retour

(none)

[modifier] Complexité

Exactement last - first incrémentations et assignations.

[modifier] Implémentation possible

template<class ForwardIt, class T>
constexpr // depuis C++20
void iota(ForwardIt first, ForwardIt last, T value)
{
    while(first != last) {
        *first++ = value;
        ++value;
    }
}

[modifier] Notes

La fonction est nommée après la fonction entière ⍳ du language de programmation APL. C'est un des composants STL qui n'ont pas été inclus dans C++98, mais qui ont été par la suite introduits en C++11.

[modifier] Exemple

L'exemple suivant applique std::shuffle à un vecteur d'itérateurs de std::list puisque std::shuffle ne peut pas être directement appliqué à std::list. std::iota est utilisé pour remplir les deux conteneurs.

#include <algorithm>
#include <iostream>
#include <list>
#include <numeric>
#include <random>
#include <vector>
 
int main()
{
    std::list<int> l(10);
    std::iota(l.begin(), l.end(), -4);
 
    std::vector<std::list<int>::iterator> v(l.size());
    std::iota(v.begin(), v.end(), l.begin());
 
    std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
 
    std::cout << "Contents of the list: ";
    for(auto n: l) std::cout << n << ' ';
    std::cout << '\n';
 
    std::cout << "Contents of the list, shuffled: ";
    for(auto i: v) std::cout << *i << ' ';
    std::cout << '\n';
}

Résultat possible :

Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5
Contents of the list, shuffled: 0 -1 3 4 -4 1 -2 -3 2 5

[modifier] Voir aussi

attribue un ensemble d'éléments d'une certaine valeur
Original:
assigns a range of elements a certain value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(fonction générique) [edit]
enregistre le résultat d'une fonction dans une plage
Original:
saves the result of a function in a range
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(fonction générique) [edit]