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 }
打印結果:
=======================================================================================================================