////////////////////////////////////////////////////////////////////// // shuffle.cpp : Illustrates how to use the random_shuffle // function. // Copyright (c) 2007 Ira Pohl All rights reserved. ////////////////////////////////////////////////////////////////////// #include #include #include #include using namespace std; template void print( Iterator i, Iterator end) { for(; i!=end ; ++i ) cout<<*i<<" "; } int main() { const int VECTOR_SIZE = 8 ; vector Numbers(VECTOR_SIZE); vector::iterator start, end, it ; // Initialize vector Numbers Numbers[0] = 4 ; Numbers[1] = 10; Numbers[2] = 70 ; Numbers[3] = 30 ; Numbers[4] = 10; Numbers[5] = 69 ; Numbers[6] = 96 ; Numbers[7] = 100; start = Numbers.begin() ; // location of first element of Numbers end = Numbers.end() ; // one past the location of last element of Numbers cout << "Before calling random_shuffle\n" << endl ; cout << "Numbers { " ; print(start, end); cout << " }\n" << endl ; // shuffle the elements in a random order random_shuffle(start, end) ; cout << "After calling random_shuffle\n" << endl ; cout << "Numbers { " ; print(start, end); cout << " }\n" << endl ; int data[8] = {1,2,3,4,5,6,7,8}; cout << "Before calling random_shuffle\n" << endl ; cout << "data { " ; print(data, data+8); cout << " }\n" << endl ; random_shuffle(data, data + 8); cout << "After calling random_shuffle\n" << endl ; cout << "data { " ; print(data, data+8); cout << " }\n" << endl ; }