C++ examples for STL Algorithm:random_shuffle
Shuffle a sequence with random_shuffle()
#include <iostream> #include <vector> #include <algorithm> using namespace std; void show(const char *msg, vector<int> vect); int main()//from ww w.j av a2 s. c o m { vector<int> v; for(int i=0; i<10; i++) v.push_back(i); show("Original order: ", v); cout << endl; // Randomize v. random_shuffle(v.begin(), v.end()); show("After shuffle: ", v); return 0; } void show(const char *msg, vector<int> vect) { cout << msg; for(unsigned i=0; i < vect.size(); ++i) cout << vect[i] << " "; cout << "\n"; }