1: //vector12.cpp 3: #include <iostream> 4: #include <vector> 5: #include <string> 6: using namespace std; 8: int main() 9: { 10: cout << "\nThis program illustrates the use of member functions " 11: "\nclear(), erase(), and pop_back() for vector objects."; 12: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 14: int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 15: vector<int> v(a, a+10); 16: cout << "\nFor v we have ..."; 17: cout << "\nSize = " << v.size() << " Capacity = " << v.capacity(); 18: cout << "\nContents: "; 19: for(vector<int>::size_type i=0; i<v.size(); i++) 20: cout << v.at(i) << " "; 21: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 23: cout << "\nFirst, we delete the third component of v using this " 24: "call to erase():\np = v.erase(v.begin()+2);" 25: "\nand then display the remaining values in v."; 26: vector<int>::iterator p = v.erase(v.begin()+2); 27: cout << "\nSize = " << v.size() << " Capacity = " << v.capacity(); 28: cout << "\nContents: "; 29: for(vector<int>::size_type i=0; i<v.size(); i++) 30: cout << v.at(i) << " "; 31: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 32: cout << "\nThe iterator returned by the above call to erase() " 33: "points to " << *p << ".\n"; 34: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 36: cout << "\nNext, we delete the 3rd through 7th components of v using " 37: "this call to erase():\np = v.erase(v.begin()+2, v.begin()+7);" 38: "\nand then display the remaining values in v."; 39: p = v.erase(v.begin()+2, v.begin()+7); 40: cout << "\nSize = " << v.size() << " Capacity = " << v.capacity(); 41: cout << "\nContents: "; 42: for(vector<int>::size_type i=0; i<v.size(); i++) 43: cout << v.at(i) << " "; 44: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 45: cout << "\nThe iterator returned by the above call to erase() " 46: "points to " << *p << ".\n"; 47: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 49: cout << "\nNow we remove a component from the end of v (using " 50: "v.pop_back()),\nand display again to confirm."; 51: v.pop_back(); 52: cout << "\nSize = " << v.size() << " Capacity = " << v.capacity(); 53: cout << "\nContents: "; 54: for(vector<int>::size_type i=0; i<v.size(); i++) 55: cout << v.at(i) << " "; 56: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 58: cout << "\nFinally, we clear v and display once again to confirm:"; 59: v.clear(); 60: cout << "\nSize = " << v.size() << " Capacity = " << v.capacity(); 61: cout << "\nContents: "; 62: for(vector<int>::size_type i=0; i<v.size(); i++) 63: cout << v.at(i) << " "; 64: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 65: }