// remove4.cpp - Erase all the zero elements in a vector. // This is an effective method. #include #include #include using namespace std; void showVec(const vector &v) { cout << "\n "; int i; for(i=0; i v; v.push_back(5); v.push_back(1); v.push_back(0); v.push_back(-1); v.push_back(0); v.push_back(3); v.push_back(0); v.push_back(0); v.push_back(-4); v.push_back(0); showVec(v); vector::iterator new_end; new_end = remove(v.begin(),v.end(),0); v.erase(new_end,v.end()); showVec(v); } 5, 1, 0, -1, 0, 3, 0, 0, -4, 0 5, 1, -1, 3, -4