// File: slist2.cpp // // Simple use of STL list container class // Derive SortedList class from STL list class // #include #include #include using namespace std ; template class SortedList : public list { public: void add(T x) ; } ; template void SortedList::add(T x) { typename SortedList::iterator it ; it = this->begin() ; while ( it != this->end() ) { if (! (x < *it) ) { break ; } it++ ; } insert(it,x) ; } const unsigned long int seed = 38913154 ; //const int reps = 10000 ; // ten thousand const int reps = 20000 ; // twenty thousand //const int reps = 200000 ; // two *hundred* thousand //const int reps = 400000 ; // four *hundred* thousand int main() { SortedList L ; SortedList::iterator it ; int x, found = 0 ; srandom(seed) ; for (int i = 0 ; i < reps ; i++) { L.add(random()) ; } for (int i = 0 ; i < reps ; i++) { x = random() ; for(it = L.begin() ; it != L.end() ; it++) { if (x == *it) { found++ ; break ; } } } cout << "Number of random numbers found: " << found << "\n" ; }