//--------------------------------------------------- // File: VectorExample.cpp // // Other required file header stuff here // // An example program to illustrate the use of vectors // //---------------------------------------------------- #include #include #include using namespace std; int main ( ) { // an empty vector of strings vector< string > names; // size and capacity should be zero cout << "size = " << names.size() << endl; cout << "capacity = " << names.capacity() << endl; /********************************************* //------------ // get names from the user string name; const int NRNAMES = 4; for (int i = 0; i < NRNAMES; i++) { cout << "Input name: "; cin >> name; names.push_back( name ); } // print the size, capacity and contents cout << endl; cout << "size = " << names.size() << endl; cout << "capacity = " << names.capacity() << endl; for (unsigned int i = 0; i < names.size(); i++) cout << names.at(i) << endl; //------------- // one more name, then print again cout << endl << "Input name: "; cin >> name; names.push_back( name ); cout << endl; cout << "size = " << names.size() << endl; cout << "capacity = " << names.capacity() << endl; for (unsigned int i = 0; i < names.size(); i++) cout << names.at(i) << endl; //--------------- // remove the 2nd name // thenprint the size, capacity and contents cout << "\nRemoving 2nd name\n"; names.erase( names.begin( ) + 1); cout << "size = " << names.size() << endl; cout << "capacity = " << names.capacity() << endl; for (unsigned int i = 0; i < names.size(); i++) cout << names[i] << endl; //-------------------------- // make the vectory bigger // 20 is an arbitrary value for example purposes cout << "\nMake the capacity bigger\n"; names.reserve( 20 ); cout << "size = " << names.size() << endl; cout << "capacity = " << names.capacity() << endl; for (unsigned int i = 0; i < names.size(); i++) cout << names[i] << endl; //--------------------- // now resize to make it smaller // 2 is arbitrary for example purposes cout << "\nMake the size smaller\n"; names.resize( 2 ); cout << "size = " << names.size() << endl; cout << "capacity = " << names.capacity() << endl; for (unsigned int i = 0; i < names.size(); i++) cout << names.at(i) << endl; **********************************************/ return 0; }