◐ Shell
clean mode source ↗

std::generate - cppreference.com

Da cppreference.com.

Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.

La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui.

Click here for the English version of this page

<metanoindex/>

<tbody> </tbody>

Elemento definito nell'header

<algorithm>

template< class ForwardIt, class Generator > void generate( ForwardIt first, ForwardIt last, Generator g );

Assegna ogni elemento nella gamma [first, last) un valore generato dall'oggetto funzione data g.

Original:

Assigns each element in range [first, last) a value generated by the given function object g.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Parametri

first, last -

l'intervallo di elementi da generare

Original:

the range of elements to generate

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

g - generator function object that will be called.

The signature of the function should be equivalent to the following:

<tbody> </tbody>

The type Ret must be such that an object of type ForwardIt can be dereferenced and assigned a value of type Ret. ​

Type requirements
-ForwardIt must meet the requirements of ForwardIterator.

Valore di ritorno

(Nessuno)

Original:

(none)

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Complessità

Esattamente std::distance(first, last) invocazioni di g() e le assegnazioni.

Original:

Exactly std::distance(first, last) invocations of g() and assignments.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Possibile implementazione

template<class ForwardIt, class Generator>
void generate(ForwardIt first, ForwardIt last, Generator g)
{
    while (first != last) {
        *first++ = g();
    }
}

Esempio

Gli usi codice seguente riempie un vettore con numeri casuali:

Original:

The following code uses fills a vector with random numbers:

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <algorithm>
#include <iostream>
#include <cstdlib>
 
int main()
{
    std::vector<int> v(5);
    std::generate(v.begin(), v.end(), std::rand); // Using the C function rand()
 
    std::cout << "v: ";
    for (auto iv: v) {
        std::cout << iv << " ";
    }
    std::cout << "\n";
}

Output:

v: 52894 15984720 41513563 41346135 51451456

Vedi anche

assegna un intervallo di elementi di un certo valore

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.


(funzione di modello) [modifica]

salva il risultato di applicazioni N di una funzione

Original:

saves the result of N applications of a function

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(funzione di modello) [modifica]