Precautions:
Pay special attention to the use of two iterators at any time to generate a front-closed interval (see examples of insertions and deletions)
It is important to note that begin () points to the No. 0 element in the VEC, and end is the next position (not the last element) that points to the last element.
Pay particular attention to the timeliness of iterators, which can have unintended consequences if the content pointed to by an iterator has been deleted and then used.
1//Basic operation of the container2//Pay special attention to the use of two iterators at any time to generate a front-closed interval (see examples of insertions and deletions)3//It is important to note that begin () points to the No. 0 element in the VEC, and end is the next position (not the last element) that points to the last element.4//Pay particular attention to the timeliness of iterators, which can have unintended consequences if the content pointed to by an iterator has been deleted and then used.5 #include <iostream>6 #include <vector>7 #include <algorithm>8UsingNamespaceStd9void Print (vector<Int>Vec10{11Forint i=0;i<vec.size (); i++)12{cout<<vec[i]<<"";14}cout<<Endl16}1718IntMain ()19{20//initialization, general sequence container initialization capacity, content, associative container initialization comparison method21stint num[10]={0,1,2,3,4,5,6,7,8,9};Vector <Int> VEC (num,num+10);23Print (VEC);2425//Add an element insert (to use an iterator, insert the data at the specified position in the iterator, note the string distinction), push26//Special note that if you use two iterators, it will be a front-closed intervalvector<Int>:: Iterator Iter_int;28//Insert 99 on the position of the second element (Vec[1])Iter_int=vec.begin () +1;Vec.insert (Iter_int,99);cout<<"Insert 99 on the position of the second element (Vec[1])"<<Endl32Print (VEC);33//Insert 3 in the position of the third element (Vec[2]) 100Iter_int=vec.begin () +2;Vec.insert (Iter_int,3,100);Approx. cout<<"Insert three in the position of the third element (Vec[1]) 100"<<Endl37Print (VEC);vector<Int>temp (num,num+10);cout<<"Temp"<<Endl40Print (temp);vector<Int>::iterator T1=temp.begin () +1;vector<Int>::iterator T2=temp.begin () +3;43Vec.insert (Vec.begin (), t1,t2);cout<<"In the position of the 1th element (Vec[0]), the VEC is inserted after the [1,3] of temp:";45Print (VEC);46cout<<"Push insert operation"<<EndlVec.push_back (100);49Print (VEC);5051//Delete element Erase, clear (delete all)52//Special note that if you use two iterators, it will be a front-closed intervalIter_int=Vec.begin ();Vec.erase (iter_int,iter_int+5);cout<<"Delete operation, delete the contents of [0,5] Interval"<<Endl56Print (VEC);5758//Find operations (the sequence container does not have a lookup operation, only the associative container is used)5960//Modify the element, modify the element first to find the element to be modified, if the lookup return reference directly to modify the reference, if the return is an iterator, you need to use the Iterator method to modify the value of the elementcout<<"modifying element actions"<<Endlvec[0]=8;//[] Returns a reference, so the direct operation63Print (VEC);Iter_int=Vec.begin ();*iter_int=9;//Iterators need to modify the value of an element using an iterator method66Print (VEC);6768//Swap element swap with the specified containercout<<"Exchange Content"<<EndlVector <Int> VEC1 (num,num+10);cout<<"The element VEC before swapping:";72Print (VEC);cout<<"Pre-swap element VEC1:";74Print (VEC1);75Vec1.swap (VEC);cout<<"Post-swap element VEC:";77Print (VEC);cout<<"Post-swap element VEC1:";79Print (VEC1);8081//Can be compared between containers, as compared to a string82if (vec>VEC1)83{cout<<"Vec>vec1"<<Endl85}86Else87{cout<<"Vec<=vec1"<<Endl89}9091//Sortcout<<"Sort the VEC1"<<EndlIter_int=Vec1.begin ();94 Sort (iter_int,iter_int+Vec1.size ());95Print (VEC1);9697//Begin () End ()98//It is important to note that begin () points to the No. 0 element in the VEC, and end is the next position (not the last element) that points to the last element.cout<<"Begin and End" <<Endl; iter_int=Vec1.begin (); 101 cout<<*iter_int<<Endl; 102 Iter_int=vec1.end ()-1; if written in this way is wrong iter_int=vec1.end (); 103 cout<<*iter_int<<Endl; 104 return 0; (+})
Basic operation of C + + STL container