/* testvector.cpp CIS 150 David Klick 7/1/08 Demonstration of STL vector class. */ #include #include #include #include using std::cin; using std::cout; using std::vector; void cont(); int main() { vector v; // create a vector int i, n; // randomize random number generator srand(time(NULL)); // put 10 random numbers (1-100) into vector for (i=0; i<10; i++) v.push_back(rand() % 100 + 1); // display vector values using at() function for (i=0; i<10; i++) cout << v.at(i) << ' '; cout << '\n'; // display vector values using [] syntax for (i=0; i<10; i++) cout << v[i] << ' '; cout << '\n'; // clear vector one element at a time (from end) while (!v.empty()) { n = v.back(); v.pop_back(); cout << "removed " << n << '\n'; } cout << "Vector now has size " << v.size() << '\n'; cont(); return 0; } /* void cont(); Arguments: none Returns: nothing Clears input buffer and pauses waiting for user to press Enter. */ void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); }