#include <iostream># Include<vector>using namespace std;vector<int>ls;void GQ () {for (int i=0;i<10;i++) ls.push_back (i);} int main () {cout<<ls.capacity () <<endl;//is defined after the container occupies memory size GQ ();cout<< "ls.capacity () =" << Ls.capacity () << "ls.size () =" <<ls.size () <<endl;ls.clear (); for (int a=11;a<13;a++) Ls.push_back (a);cout<< "ls.capacity () =" <<ls.capacity () << "ls.size () =" <<ls.size () <<endl;// The discovery simply placed the container size//at 0 and did not release the memory. for (int j=0;j<10;j++)//Because the memory is not released find that the element value is not changed and clear (), the iterator points to the first element cout<<ls[j]<<endl;//only 11 12 2 3-- Cover the first two elements. At this time LS. End () returns an iterator that points to 12. Cout<<endl<<endl;ls.push_back (ls.push_back); Vector<int>::iterator It=ls.begin (+3); for (; It!=ls.end ();)//local element is deleted, the subsequent elements move forward as a whole, and the iterator logically points to the second element. {Ls.erase (it);} It self-increment 1;for (Vector<int>::iterator iter=ls.begin (); Iter!=ls.end (); iter++) cout<< "Lingwai =" <<* iter<<endl;cout<< "ls.capacity () =" <<ls.capacity () << "ls.size () =" <<ls.size () < <endl;gq ();vector<int> (). Swap (LS),//swap, clear the container and reclaim the space//for (int m=0;m<10;m++)//If not annotated, then runtime error! cout<<ls[m]<<endl;cout<< "ls.capacity () =" <<ls.capacity () << "ls.size () =" << Ls.size () <<endl;return 0;}
About the use cases and principles of the erase () function in an array container