Mega Code Archive

 
Categories / C++ / Algorithm
 

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; }