C++ examples for STL Algorithm:random_shuffle
Shuffle element in vector with random_shuffle
#include <iostream> #include <algorithm> // algorithm definitions #include <numeric> // accumulate is defined here #include <vector> #include <iterator> using namespace std; int main() /*from w ww . j a va 2 s. com*/ { const int SIZE = 10; int a1[ SIZE ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; vector< int > v( a1, a1 + SIZE ); // copy of a1 ostream_iterator< int > output( cout, " " ); cout << "Vector v before random_shuffle: "; copy( v.begin(), v.end(), output ); random_shuffle( v.begin(), v.end() ); // shuffle elements of v cout << "\nVector v after random_shuffle: "; copy( v.begin(), v.end(), output ); }