std::random_shuffle, std::shuffle — cppreference.com
De cppreference.com
<metanoindex/>
<tbody> </tbody>
| Déclaré dans l'en-tête <algorithm> |
||
|
|
(1) | |
|
|
(2) | (avant C++11) (depuis C++11) |
|
|
(3) | (depuis C++11) |
Réordonne les éléments de la gamme proposée [first, last) de sorte que chaque permutation possible de ces éléments a une probabilité égale d'apparence .
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.
1)
Le générateur de nombres aléatoires est définie par l'implémentation, mais le std::rand fonction est souvent utilisée .
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.
2)
Le générateur de nombres aléatoires est l'objet r fonction .
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.
3)
Le générateur de nombres aléatoires est l'objet g fonction .
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.
Paramètres
| first, last | - | la plage d'éléments à mélanger de manière aléatoire Original: the range of elements to shuffle randomly The text has been machine-translated via Google Translate. |
| r | - | objet fonction retournant une valeur choisie au hasard de type convertible en Original: function object returning a randomly chosen value of type convertible to The text has been machine-translated via Google Translate. |
| g | - | objet fonction retournant une valeur choisie au hasard de Original: function object returning a randomly chosen value of type The text has been machine-translated via Google Translate. |
| Type requirements | ||
-RandomIt must meet the requirements of ValueSwappable and RandomAccessIterator.
| ||
-URNG must meet the requirements of UniformRandomNumberGenerator.
| ||
Retourne la valeur
(Aucun)
Original:
(none)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Complexité
linéaire de la distance entre first et 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.
Mise en œuvre possible
| 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))]); } } |
Exemple
Le code suivant modifie de façon aléatoire les entiers de 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.
#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"; }
Sortie possible:
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.
Voir aussi
génère la plus grande lexicographique prochaine permutation d'un ensemble d'éléments Original: generates the next greater lexicographic permutation of a range of elements The text has been machine-translated via Google Translate. (fonction générique) [edit] | |
lexicographique génère le plus petit côté d'une permutation série d'éléments Original: generates the next smaller lexicographic permutation of a range of elements The text has been machine-translated via Google Translate. (fonction générique) [edit] | |