// File: svector2.cpp // // Simple use of STL vector container class // Derives SortedVector from vector // #include #include #include #include #include using namespace std ; template class SortedVector : public vector { public: void add(T x) ; } ; template void SortedVector::add(T x) { typename SortedVector::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() { SortedVector V ; SortedVector::iterator it ; int x, found = 0 ; srandom(seed) ; for (int i = 0 ; i < reps ; i++) { V.add(random()) ; } for (int i = 0 ; i < reps ; i++) { x = random() ; if (binary_search(V.begin(), V.end(), x) ) found++ ; } cout << "Number of random numbers found: " << found << "\n" ; }