About the use cases and principles of the erase () function in an array container

Source: Internet
Author: User

 #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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.