思路:直接调用STL函数,当然速度并不会太快
至于这两个函数的用法,这个里面有图解
Vector中删除元素
remove(v.begin(), v.end(), 99); //remove有返回值 , newEnd位置
iterator erase (iterator position);
iterator erase (iterator first, iterator last);//这个删除的范围是左闭右开
// erase the 6th element
myvector.erase (myvector.begin()+5);
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);
# include<iostream>
# include<vector>
# include<string>
# include<algorithm>
# include<math.h>
# include<climits>
# include<stack>
# include<queue>
using namespace std;
int removeElement(vector<int>& nums, int val) {
nums.erase(remove(nums.begin(), nums.end(), val), nums.end());
return nums.size();
}
int main(void) {
vector<int> nums;
nums.push_back(1);
nums.push_back(1);
nums.push_back(2);
nums.push_back(3);
nums.push_back(4);
cout << removeElement(nums,1) << endl;
system("pause");
return 0;
}