std::random_shuffle a vector : random_shuffle « STL Algorithms Modifying sequence operations « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>

int main()
{
   int a1[ 10 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   std::vector< int > v( a1, a1 + 10 ); // copy of a1
   std::ostream_iterator< int > output( cout, " " );

   cout << "Vector v before random_shuffle: ";
   std::copy( v.begin(), v.end(), output );

   std::random_shuffle( v.begin(), v.end() ); // shuffle elements of v
   cout << "\nVector v after random_shuffle: ";
   std::copy( v.begin(), v.end(), output );
   cout << endl;
   return 0;
}
Vector v before random_shuffle: 1 2 3 4 5 6 7 8 9 10
Vector v after random_shuffle: 9 2 10 3 1 6 8 4 5 7








24.7.random_shuffle
24.7.1.std::random_shuffle a vector
24.7.2.Use back_insert_iterator to insert element into a vector
24.7.3.Use random_shuffle to shuffle all elements randomly
24.7.4.Use random_shuffle to shuffle elements with self-written random number generator
24.7.5.Shuffle a sequence with random_shuffle()
24.7.6.shuffle the container with random_shuffle
24.7.7.Number of elements greater than 9: count_if and greater()