Randomize elements in a vector with random_shuffle(scores.begin(), scores.end()); : random_shuffle « STL Algorithms Modifying sequence operations « C++






Randomize elements in a vector with random_shuffle(scores.begin(), scores.end());

  
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    vector<int>::const_iterator iter;

    vector<int> scores;
    scores.push_back(1);
    scores.push_back(3);
    scores.push_back(5);

    for (iter = scores.begin(); iter != scores.end(); ++iter)
        cout << *iter << endl;

    srand(time(0));
    random_shuffle(scores.begin(), scores.end());
    for (iter = scores.begin(); iter != scores.end(); ++iter)
        cout << *iter << endl;

 return 0;
}
  
    
  








Related examples in the same category

1.std::random_shuffle a vector
2.Use back_insert_iterator to insert element into a vector
3.Use random_shuffle to shuffle all elements randomly
4.Use random_shuffle to shuffle elements with self-written random number generator