1: //remove1a.cpp 3: #include <iostream> 4: #include <vector> 5: #include <algorithm> 6: using namespace std; 8: int main() 9: { 10: cout << "\nThis program illustrates the use of the STL " 11: "remove() algorithm\nto remove all copies of a given " 12: "value from a vector of integers,\nor from a subrange " 13: "of such a vector."; 14: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 16: int a[] = {1, 2, 2, 3, 4, 5, 2, 3, 6, 7, 2, 8, 3, 9, 10, 2}; 17: vector<int> v(a, a+16); 19: cout << "\nHere are the sixteen values in the vector:\n"; 20: for (vector<int>::size_type i=0; i<v.size(); i++) 21: cout << v.at(i) << " "; 22: cout << "\nSize of vector to start: " << v.size(); 23: cout << "\nCapacity of vector to start: " << v.capacity(); 24: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 26: cout << "\nNow we remove all 5 copies of the value 2."; 27: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 28: vector<int>::iterator new_end = remove(v.begin(), v.end(), 2); 29: cout << "\nSize of vector immediately after the call to remove: " 30: << v.size(); 31: cout << "\nCapacity of vector immediately after the call to remove: " 32: << v.capacity(); 33: cout << "\nContents of vector immediately after the call to remove:\n"; 34: for (vector<int>::size_type i=0; i<v.size(); i++) 35: cout << v.at(i) << " "; 36: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 38: cout << "\nNow we erase the last five values of the vector."; 39: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 40: v.erase(new_end, v.end()); 41: cout << "\nSize of vector immediately after the call to erase: " 42: << v.size(); 43: cout << "\nCapacity of vector immediately after the call to erase: " 44: << v.capacity(); 45: cout << "\nContents of vector immediately after the call to erase:\n"; 46: for (vector<int>::size_type i=0; i<v.size(); i++) 47: cout << v.at(i) << " "; 48: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 50: cout << "\nNow we remove the two copies of the value 3 from among " 51: "the first 8 values."; 52: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 53: new_end = remove(v.begin(), v.begin()+8, 3); 54: cout << "\nSize of vector immediately after the call to remove: " 55: << v.size(); 56: cout << "\nCapacity of vector immediately after the call to remove: " 57: << v.capacity(); 58: cout << "\nContents of vector immediately after the call to remove:\n"; 59: for (vector<int>::size_type i=0; i<v.size(); i++) 60: cout << v.at(i) << " "; 61: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 63: v.erase(new_end, v.begin()+8); 64: cout << "\nSize of vector immediately after the call to erase: " 65: << v.size(); 66: cout << "\nCapacity of vector immediately after the call to erase: " 67: << v.capacity(); 68: cout << "\nContents of vector immediately after the call to erase:\n"; 69: for (vector<int>::size_type i=0; i<v.size(); i++) 70: cout << v.at(i) << " "; 71: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 72: }