shuffle the container with random_shuffle : random_shuffle « STL Algorithms Modifying sequence operations « C++ Tutorial






#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main (){
    // Initialize a sample vector with 6 elements
    vector <int> v (6);

    // fill first 3 elements with value 8
    fill (v.begin (), v.begin () + 3, 8);

    // fill last 3 elements with value 5
    fill_n (v.begin () + 3, 3, 5);

    // shuffle the container
    random_shuffle (v.begin (), v.end ());

    for (size_t nIndex = 0; nIndex < v.size (); ++ nIndex){
        cout << "Element [" << nIndex << "] = ";
        cout << v [nIndex] << endl;
    }

    return 0;
}








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()