// Erasing elements from containers #include #include #include #include void show_int (int x) { cout << " " << x; } int main() { cout << "Demonstrating use of erasing elements from containers:" << endl; list list2; int foobar[9] = { 4, 5, 6, 1, 2, 3, 7, 8, 9 }; vector vector2(&foobar[0], &foobar[8]); // populating the list using "push_back" list2.push_back(89); list2.push_back(99); list2.push_back(89); list2.push_back(69); list2.push_back(59); list2.push_back(89); list2.push_back(89); list2.push_back(49); cout << "List2 initial contents: [ " ; for_each(list2.begin(), list2.end(), show_int); cout << " ]" << endl; cout << "Vector2 initial contents: < " ; for_each(vector2.begin(), vector2.end(), show_int); cout << " >" << endl; list::iterator lp; lp = find(list2.begin(), list2.end(), 59); if (lp != list2.end()) list2.erase(lp); cout << "List2 contents (after erasing 59): [ " ; for_each(list2.begin(), list2.end(), show_int); cout << " ]" << endl; lp = find(list2.begin(), list2.end(), 69); list2.erase(lp, list2.end()); cout << "List2 contents (after erasing 69 to the end of list): [ " ; for_each(list2.begin(), list2.end(), show_int); cout << " ]" << endl; vector::iterator vp; vp = find(vector2.begin(), vector2.end(), 1); if (vp != vector2.end()) vector2.erase(vp); cout << "Vector2 contents (after erase 1): < " ; for_each(vector2.begin(), vector2.end(), show_int); cout << " >" << endl; vp = find(vector2.begin(), vector2.end(), 35); if (vp != vector2.end()) vector2.erase(vp); cout << "Vector2 contents (after erase 35): < " ; for_each(vector2.begin(), vector2.end(), show_int); cout << " >" << endl; vp = find(vector2.begin(), vector2.end(), 3); vector2.erase(vp, vector2.end()); cout << "Vector2 contents (after erase 3 through end): < " ; for_each(vector2.begin(), vector2.end(), show_int); cout << " >" << endl; }