Namespace
Varianti

std::generate_n

Da cppreference.com.
< cpp‎ | algorithm

 
 
Algoritmo libreria
Funzioni
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 modifica le operazioni di sequenza
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.
Modifica delle operazioni di sequenza
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.
Partizionamento operazioni
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.
Ordinamento delle operazioni (su intervalli ordinati)
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.
Binarie (le operazioni di ricerca sui campi ordinati)
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.
Impostare le operazioni (su intervalli ordinati)
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.
Heap operazioni
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.
Minimo / massimo le operazioni
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.
Operazioni numeriche
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.
Libreria 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.
 
Defined in header <algorithm>
template< class OutputIt, class Size, class Generator >

void generate_n( OutputIt first, Size count, Generator g );
template< class OutputIt, class Size, class Generator >

OutputIt generate_n( OutputIt first, Size count, Generator g );
(C fino + 11)

(dal C++11)
Assegna valori, generati da g data funzione oggetto, agli elementi count primi all'inizio gamma a first, se count>0. Non fa niente altrimenti.
Original:
Assigns values, generated by given function object g, to the first count elements in the range beginning at first, if count>0. Does nothing otherwise.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Indice

[modifica] Parametri

first -
l'inizio dell'intervallo di elementi da generare
Original:
the beginning of 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.
count -
numero di elementi da generare
Original:
number of the 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:

Ret fun();

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

Type requirements
-
OutputIt must meet the requirements of OutputIterator.

[modifica] Valore di ritorno

(Nessuno) (C fino + 11)
Original:
(none) (C fino + 11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Iterator un passato l'ultimo elemento assegnato se count>0, first altrimenti. (dal C++11)
Original:
Iterator one past the last element assigned if count>0, first otherwise. (dal C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifica] Complessità

Esattamente count invocazioni di g() e le assegnazioni, per count>0.
Original:
Exactly count invocations of g() and assignments, for count>0.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifica] Possibile implementazione

template< class OutputIt, class Size, class Generator >
OutputIt generate_n( OutputIt first, Size count, Generator g )
{
    for( Size i = 0; i < count; i++ ) {
        *first++ = g();
    }
    return first;
}

[modifica] Esempio

Il codice seguente riempie un array di interi con numeri casuali .
Original:
The following code fills an array of integers 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 <cstddef>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <algorithm>
 
int main()
{
    const std::size_t N = 5;
    int ar[N];
    std::generate_n(ar, N, std::rand); // Using the C function rand()
 
    std::cout << "ar: ";
    std::copy(ar, ar+N, std::ostream_iterator<int>(std::cout, " "));
    std::cout << "\n";
}

Output:

52894 15984720 41513563 41346135 51451456

[modifica] Vedi anche

assegna un valore a un numero di elementi
Original:
assigns a value to a number of elements
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) [edit]
salva il risultato di una funzione in un intervallo
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.

(funzione di modello) [edit]