STL——容器(deque)deque 的删除 clear() erase()
deque.clear();
//移除容器的所有数据
1 #include <iostream> 2 #include <deque> 3 4 using namespace std; 5 6 int main() 7 { 8 int num[] = { 111,222,333,444,555 }; 9 deque<int> deqInt_A(num, num + size(num)); 10 11 cout << "deqInt_A中的元素个数为:"; 12 cout << deqInt_A.size() << endl; 13 14 cout << "deqInt_A所占用内存:"; 15 cout << sizeof(deqInt_A) << endl; 16 17 cout << "遍历deqInt_A:"; 18 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++) 19 { 20 cout << *it << " "; 21 } 22 23 //删除容器中 24 deqInt_A.clear(); 25 26 cout << "\n\nclear后,deqInt_A中的元素个数为:"; 27 cout << deqInt_A.size(); 28 cout << "\ndeqInt_A所占用内存:"; 29 cout << sizeof(deqInt_A); 30 cout << "\nclear后,遍历deqInt_A:" << endl; 31 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++) 32 { 33 cout << *it << " "; 34 } 35 36 return 0; 37 }
打印结果:
可以发现内存是并没有释放的
deque.erase(beg,end);
//删除[beg,end)区间的数据,返回下一个数据的位置。
1 #include <iostream> 2 #include <deque> 3 4 using namespace std; 5 6 int main() 7 { 8 int num[] = { 111,222,333,444,555 }; 9 deque<int> deqInt_A(num, num + size(num)); 10 11 cout << "遍历deqInt_A:"; 12 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++) 13 { 14 cout << *it << " "; 15 } 16 cout << "\ndeqInt_A中的元素个数为:"; 17 cout << deqInt_A.size() << endl; 18 19 cout << "deqInt_A所占用内存:"; 20 cout << sizeof(deqInt_A) << endl; 21 22 23 24 cout << "\n删除容器中前三个元素后,遍历deqInt_A:"; 25 //删除容器中前三个元素后,用返回的迭代器遍历,返回了下一个元素的位置 26 for (deque<int>::iterator it = deqInt_A.erase(deqInt_A.begin(), deqInt_A.begin() +3); it != deqInt_A.end(); it++) 27 { 28 cout << *it << " "; 29 } 30 cout << "\n删除容器中前三个元素后,deqInt_A中的元素个数为:"; 31 cout << deqInt_A.size(); 32 33 cout << "\n删除容器中前三个元素后,deqInt_A所占用内存:"; 34 cout << sizeof(deqInt_A); 35 36 return 0; 37 }
打印结果:
可以发现,deqInt_A中的元素删除后,占用的内存空间大小并没有变化
deque.erase(pos);
//删除pos位置的数据,返回下一个数据的位置。
1 #include <iostream> 2 #include <deque> 3 4 using namespace std; 5 6 int main() 7 { 8 int num[] = { 111,222,333,444,555 }; 9 deque<int> deqInt_A(num, num + size(num)); 10 11 cout << "遍历deqInt_A:"; 12 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++) 13 { 14 cout << *it << " "; 15 } 16 cout << "\ndeqInt_A中的元素个数为:"; 17 cout << deqInt_A.size() << endl; 18 19 cout << "deqInt_A所占用内存:"; 20 cout << sizeof(deqInt_A) << endl; 21 22 cout << "\n删除容器中第三个元素后边的元素后,用返回的迭代器遍历后边的元素:"; 23 //删除容器中第三个元素后边的元素后,用返回的迭代器遍历,返回了下一个元素的位置,注意这个数字不是第三个,是第三个之后的那个元素 24 for (deque<int>::iterator it = deqInt_A.erase(deqInt_A.begin() + 3); it != deqInt_A.end(); it++) 25 { 26 cout << *it << " "; 27 } 28 cout << "\n遍历 deqInt_A 中所有的元素:"; 29 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++) 30 { 31 cout << *it << " "; 32 } 33 cout << "\n删除容器中第三个元素后边的元素后,deqInt_A中的元素个数为:"; 34 cout << deqInt_A.size(); 35 36 cout << "\n删除容器中第三个元素后边的元素后,deqInt_A所占用内存:"; 37 cout << sizeof(deqInt_A); 38 39 return 0; 40 }
打印结果:
一般在项目中删除单个元素会这样用:
1 #include <iostream> 2 #include <deque> 3 4 using namespace std; 5 6 int main() 7 { 8 int num[] = { 111,222,333,444,555 }; 9 deque<int> deqInt_A(num, num + size(num)); 10 11 //删除等于 444 的元素 12 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end();) 13 { 14 if (*it == 444) 15 { 16 it = deqInt_A.erase(it); //删除元素后,erase 会返回下一个元素的位置,相当于 it++操作了 17 } 18 cout << *it << " "; 19 it++; //不把it++ 写到for循环的条件语句中,是为了避免删除元素后的越界访问,比如删除 444 后,555会在444的位置,这时候it++就会越界 20 } 21 22 return 0; 23 }
打印结果:
=======================================================================================================================