容器的删除
#include <deque> #include <vector> #include <iostream> #include <algorithm> using namespace std; void myprint(deque<int> num1) {//auto不能当函数形参 for (auto i : num1) cout << i<<" "; cout << endl; } int main() { int num[] = {9,5,2,2,2,6,4,8,45,63,2,48 }; vector<int> num0(num, num+sizeof(num)/sizeof(int)-1);//vector的初始化 cout << sizeof(num) << endl; deque<int> num1(num, num + sizeof(num) / sizeof(int) - 1); myprint(num1); cout << num1.size() << endl; //num1.erase(find(num1.begin(),num1.end(),2));//erase删除后后面元素往前移动,就消失了,不会占位 //remove(num1.begin(), num1.end(), 2);//remove删除n个元素后,后面的数依次往前填充留下的空位,末尾的后n个元素移动后留下的空位保持原样,不会删除,即倒数n个元素变成两份; cout << num1.size() << endl; auto fin=find(num1.begin(), num1.end(), 2); while (find(num1.begin(), num1.end(), 2) != num1.end()) num1.erase(find(num1.begin(), num1.end(), 2));//find查找失败返回迭代器num.end(); cout << num1.size() << endl; num1.push_back(2); cout << num1.size() << endl; myprint(num1); }