Sequential Container (ii)--"C + + Primer"

Source: Internet
Author: User



The following is a brief introduction to the definition of sequential containers and some operations, including (add, delete, etc.), the main focus in the form of code to briefly introduce the relevant usage, first create and initialize the vector object, generally there are two major categories:

1, assign the specified number of elements, and initialize (in two ways)
Vector<int> Ivec (10);
Vector<int> Ivec (10,1);
2. Initialize the Vecotr object to a copy of the element
int a[10]={1,2,3,4,5,6,7,8,9,10};
Vector<int> Ivec (A,A+10);
Initializes a vector object to a copy of another object
Vector<int> IVEC1 (10,1);
Vector<int> ivec2 (IVEC1);

For example:

#include <iostream> #include <vector> #include <string>using namespace Std;int main () {    int a[10]= {1,2,3,4,5,6,7,8,9,10};    Vector<int> Kvec (a,a+8);    Vector<int> Ivec (10,1);    Vector<int> Kvec (Ivec);    For (Vector<int>::iterator St=kvec.begin (); St!=kvec.end (); ++st)       cout <<*st << ';    cout << Endl;     return 0;}


Swap (C1,C2) is an assignment operation that swaps the elements of a c1,c2, assuming that C1,C2 must have the same type. Using swap assignments is usually more than c1=c2.swap exchanging two containers of internal data structures, whose elements themselves are not exchanged, which means that the iterator that points to the container , references and pointers do not expire after the swap operation. Then after the swap is executed, the post-swap element can be accessed through the container's iterator, such as:

#include <iostream> #include <vector> #include <string>using namespace Std;int main () {   vector <int> IVEC1 (10,1);   Vector<int> ivec2 (10,2);   Swap (IVEC1,IVEC2);   For (Vector<int>::iterator Kk=ivec1.begin (); Kk!=ivec1.end (); ++kk)      cout << *kk << Endl;   return 0;}


Resize (n) is used to change the size of the container, and when the container is enlarged, the part that is exceeded is initialized to 0 by default, and if the container is scaled down, the elements that are exceeded are deleted, such as:

#include <iostream> #include <vector> #include <string>using namespace Std;int main () {   int a[10]= {0,1,2,3,4,5,6,7,8,9};   Vector<int> Ivec (a,a+10);   Ivec.resize (a);   Ivec.resize (5);   For (Vector<int>::iterator Test=ivec.begin (); Test!=ivec.end (); ++test)      cout << *test;   cout << Endl;     return 0;}


The iterator is invalidated after the delete element operation, so be sure to reassign the iterator and update the iterator. If the delete operation is frequent, list is generally selected.

#include <iostream> #include <vector> #include <string>using namespace Std;int main () {   int a[10]= {0,1,2,3,4,5,6,7,8,9};   Vector<int> Ivec (a,a+10);     For (Vector<int>::iterator Test=ivec.begin (); Test!=ivec.end (); ++test) {      if (*test%2==0) {       test= Ivec.erase (test);       --test;      }   }   For (Vector<int>::iterator Test=ivec.begin (); Test!=ivec.end (); ++test) {      cout<< *test << ';   }    cout << Endl;  return 0;}

Here is one of the people wrote, I feel better to explain, so put together:

#include <iostream> #include <list> #include <vector> #include <deque> using namespace std; void print (List<int>::iterator beg,list<int>::iterator end) {while (beg!=end) {cout<<         *beg<< "";                     beg++; } cout<<endl; } void Print (Vector<int>::iterator beg,vector<int>::iterator end) {while (beg!=end) {cout&         lt;<*beg<< "";                     beg++; } cout<<endl;      } void Deleteelem (List<int>&lst,int flag) {int base= (flag%2==0?0:1);      List<int>::iterator Head=lst.begin (), Tail=lst.end ();                    while (Head!=tail)//There is no need to update the iterator that points to the end of the container//Because the list container does not appear when the element is moved {if (*head%2==base)                Head=lst.erase (head);      Clever use of function return value else head++; }} void Deleteelem (Vector<int>&vec,int flag) {INT base= (flag%2==0?0:1);      Vector<int>::iterator Head=vec.begin (), Tail=vec.end (); while (head!= (Tail=vec.end ()))//The iterator that points to the end of the container is updated here//because the vector container deletes the element after the element is moved forward is the iterator invalidation {if (*head                                 %2==base) Head=vec.erase (head);      else head++;     }} int main () {int ia[10]={0,1,2,3,4,5,6,7,8,9};      List<int>odd (IA,IA+10);     Vector<int>even (IA,IA+10);     Print (Odd.begin (), Odd.end ());     Print (Even.begin (), Even.end ());     Deleteelem (odd,2);     Print (Odd.begin (), Odd.end ());     Deleteelem (even,1);       Print (Even.begin (), Even.end ()); return 0;  }






Sequential Container (ii)--"C + + Primer"

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.