1: //map12.cpp 3: #include <iostream> 4: #include <map> 5: #include <utility> 6: using namespace std; 8: int main() 9: { 10: cout << "\nThis program illustrates the clear() and erase() member " 11: "functions\nof the map interface."; 12: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 14: //Create an array of pairs 15: pair<char, int> a[] = 16: { 17: pair<char, int>('A', 65), 18: pair<char, int>('B', 66), 19: pair<char, int>('C', 67), 20: pair<char, int>('D', 68), 21: pair<char, int>('E', 69), 22: pair<char, int>('F', 70), 23: pair<char, int>('G', 71), 24: pair<char, int>('H', 72), 25: pair<char, int>('I', 73), 26: pair<char, int>('J', 74) 27: }; 29: cout << "\nWe begin with a map containing the following pairs:\n"; 30: map<char, int> m(a, a+10); 31: map<char, int>::iterator p = m.begin(); 32: while (p != m.end()) 33: { 34: cout << p->first << " " << p->second << endl; 35: ++p; 36: } 37: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 39: cout << "\nThen we delete any pairs having a key of F."; 40: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 42: map<char, int>::size_type numberDeleted = m.erase('F'); 43: cout << "\nThe number of deletions was " << numberDeleted << "."; 44: cout << "\nAnd here are the remaining contents of the map:\n"; 45: p = m.begin(); 46: while (p != m.end()) 47: { 48: cout << p->first << " " << p->second << endl; 49: ++p; 50: } 51: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 53: cout << "\nNext we delete any pairs having a key of T."; 54: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 56: numberDeleted = m.erase('T'); 57: cout << "\nThe number of deletions was " << numberDeleted << "."; 58: cout << "\nAnd here are the remaining contents of the map:\n"; 59: p = m.begin(); 60: while (p != m.end()) 61: { 62: cout << p->first << " " << p->second << endl; 63: ++p; 64: } 65: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 67: cout << "\nNow we delete the first pair of the map, using an iterator " 68: "for access."; 69: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 71: m.erase(m.begin()); 72: cout << "\nThe remaining contents of the map are now these:\n"; 73: p = m.begin(); 74: while (p != m.end()) 75: { 76: cout << p->first << " " << p->second << endl; 77: ++p; 78: } 79: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 81: cout << "\nFor our final deletion before clearing all the remaining " 82: "pairs, we delete those\npairs having keys in the range from " 83: "capital letter D to capital letter G."; 84: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 86: m.erase(m.find('D'), m.find('H')); 87: cout << "\nAfter deleting this range of values, the remaining pairs " 88: "in the map are:\n"; 89: p = m.begin(); 90: while (p != m.end()) 91: { 92: cout << p->first << " " << p->second << endl; 93: ++p; 94: } 95: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 97: cout << "\nFinally, we clear all remaining values from the map."; 98: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 100: m.clear(); 101: if (m.empty()) cout << "\nThe map is now empty."; 102: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 103: }