鉴于每次打开的速度都很慢。。From http://www.cplusplus.com/reference/algorithm/random_shuffle/
std::random_shuffle
<algorithm>
generator by default (1) | template <class RandomAccessIterator> void random_shuffle (RandomAccessIterator first, RandomAccessIterator last); |
---|---|
specific generator (2) | template <class RandomAccessIterator, class RandomNumberGenerator> void random_shuffle (RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator&& gen); |
Randomly rearrange elements in range
Rearranges the elements in the range
[first,last)
randomly.
The function swaps the value of each element with that of some other randomly picked element. When provided, the function gen determines which element is picked in every case. Otherwise, the function uses some unspecified source of randomness.
To specify a uniform random generator as those defined in
<random>
, see
shuffle.
The behavior of this function template (2) is equivalent to:
| |
Parameters
-
first, last
-
Random-access iterators to the initial and final positions of the sequence to be shuffled. The range used is
[first,last)
, which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
gen
-
Unary function taking one argument and returning a value, both convertible to/from the appropriate
difference type used by the iterators. The function shall return a non-negative value less than its argument.
This can either be a function pointer or a function object.
RandomAccessIterator shall point to a type for which swap is defined and swaps the value of its arguments.
Return value
noneExample
| |
Possible output:
myvector contains: 3 4 1 6 8 9 2 7 5 |
Complexity
Linear in the distance between first and last minus one: Obtains random values and swaps elements.Data races
The objects in the range[first,last)
are modified.
Exceptions
Throws if any of the random number generations, the element swaps or the operations on iterators throws.Note that invalid arguments cause undefined behavior.