/* show simple container, algorithm, iterator example */ #include #include #include // get sort #include // for a seed #include // random number generator using namespace std; void main(){ srand(time(0)); // randomize random number sequence int arr[20]; // array as a container // initialize array randomly for(int i = 0; i < 20; i++) arr[i]= rand()%1000; // use the sort algorithm // use pointers to beginning and end as iterators sort(arr, arr+20); // show sorted result for(int i = 0; i < 20; i++) cout << arr[i] << endl; // now use a vector cout << "second example using vector " << endl; vector v(20); // declare a vector // randomly load the vector with data for(int i = 0; i < 20; i++) v[i]= rand()%1000; // tie vector to sort via begin and end iterators sort(v.begin(), v.end()); // declare an iterator - in this case, the container // vector defines the iterator vector::iterator iv; // iterate through the vector container for(iv = v.begin(); iv != v.end(); iv++) cout << *iv << endl; // use dereference operator overload of iterator }