std::random_shuffle, std::shuffle
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. |
Elemento definito nell'header <algorithm>
|
||
template< class RandomIt > void random_shuffle( RandomIt first, RandomIt last ); |
(1) | |
template< class RandomIt, class RandomFunc > void random_shuffle( RandomIt first, RandomIt last, RandomFunc& r ); |
(2) | (fino al c++11) (dal C++11) |
template< class RandomIt, class URNG > void shuffle( RandomIt first, RandomIt last, URNG&& g ); |
(3) | (dal C++11) |
Riordina gli elementi nell'intervallo dato
[first, last)
tali che ogni permutazione possibile di tali elementi ha la stessa probabilità di apparizione.Original:
Reorders the elements in the given range
[first, last)
such that each possible permutation of those elements has equal probability of appearance.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1)
Il generatore di numeri casuali è l'implementazione definita, ma la std::rand funzione viene spesso utilizzata.
Original:
The random number generator is implementation-defined, but the function std::rand is often used.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2)
Il generatore di numeri casuali è l'oggetto funzione
r
. Original:
The random number generator is the function object
r
. The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3)
Il generatore di numeri casuali è l'oggetto funzione
g
.Original:
The random number generator is the 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.
You can help to correct and verify the translation. Click here for instructions.
Indice |
[modifica] Parametri
first, last | - | la gamma di elementi per mescolare in modo casuale
Original: the range of elements to shuffle randomly The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
r | - | oggetto funzione che restituisce un valore scelto a caso di tipo convertibile
iterator_traits<RandomIt>::difference_type nell'intervallo [0, n) se invocato come r(n) Original: function object returning a randomly chosen value of type convertible to iterator_traits<RandomIt>::difference_type in the interval [0,n) if invoked as r(n) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
g | - | oggetto funzione che restituisce un valore scelto a caso di
URNG::result_type tipo nell'intervallo [g.min (), g.max ()] se invocato come g() (ad esempio, uno dei generatori di numeri casuali uniformi da <random> ) Original: function object returning a randomly chosen value of type URNG::result_type in the interval [g.min(), g.max()] if invoked as g() (e.g. any of the uniform random number generators from <random> ) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Type requirements | ||
-RandomIt must meet the requirements of ValueSwappable and RandomAccessIterator .
| ||
-URNG must meet the requirements of UniformRandomNumberGenerator .
|
[modifica] 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.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Complessità
lineare la distanza tra
first
e last
Original:
linear in the distance between
first
and last
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Possibile implementazione
First version |
---|
template<class RandomIt, class RandomFunc> void random_shuffle(RandomIt first, RandomIt last, RandomFunc&& r) { typename std::iterator_traits<RandomIt>::difference_type i, n; n = last - first; for (i = n-1; i > 0; --i) { using std::swap; swap(first[i], first[r(i+1)]); } } |
Second version |
template<class RandomIt, class UniformRandomNumberGenerator> void shuffle(RandomIt first, RandomIt last, UniformRandomNumberGenerator&& g) { typedef typename std::iterator_traits<RandomIt>::difference_type diff_t; typedef typename std::make_unsigned<diff_t>::type udiff_t; typedef typename std::uniform_int_distribution<udiff_t> distr_t; typedef typename distr_t::param_type param_t; distr_t D; diff_t n = last - first; for (diff_t i = n-1; i > 0; --i) { using std::swap; swap(first[i], first[D(g, param_t(0, i))]); } } |
[modifica] Esempio
Il codice seguente mescola casualmente i numeri interi 1 .. 10:
Original:
The following code randomly shuffles the integers 1..10:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <random> #include <algorithm> #include <iterator> #include <iostream> int main() { std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; std::random_device rd; std::mt19937 g(rd()); std::shuffle(v.begin(), v.end(), g); copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << "\n"; }
Uscita possibile:
Original:
Possible output:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
8 6 10 4 2 3 7 1 9 5
[modifica] Vedi anche
generates the next greater lexicographic permutation of a range of elements (funzione di modello) | |
generates the next smaller lexicographic permutation of a range of elements (funzione di modello) |