Part of the move from: C + + vector usage
In C + +, vector is a very useful container, and here is a summary of the container.
1. Basic operation
(1) header file #include<vector>.
(2) Create vector object,vector<int> VEC;
(3) Insert number at tail: vec.push_back (a);
(4) Use subscript to access the element,cout<<vec[0]<<endl; remember that the subscript is starting from 0.
(5) Use iterators to access elements.
Vector<int>::iterator it;
For (It=vec.begin (); It!=vec.end (); it++)
cout<<*it<<endl;
(6) Insert element: Vec.insert (Vec.begin () +i,a); Insert a in front of the first I+1 element;
(7) Delete element: Vec.erase (Vec.begin () +2); Delete 3rd element
Vec.erase (Vec.begin () +i,vec.end () +j); Delete interval [i,j-1]; interval starting from 0
(8) Vector size: vec.size ();
(9) Empty: Vec.clear ();
2. Structural body
Vector elements can not only make int,double,string, but also the structure, but note that the structure should be defined as global, otherwise it will be wrong. Here is a short program code:
#include <stdio.h>#include<algorithm>#include<vector>#include<iostream>using namespaceStd;typedefstructrect{intID; intlength; intwidth; //for vector elements that are structural, you can define a comparison function within the structure, sorted in ascending order of Id,length,width. BOOL operator< (ConstRect &a)Const { if(id!=a.id)returnid<a.id; Else { if(length!=a.length)returnlength<a.length; Else returnwidth<A.width; }}}rect;intmain () {vector<Rect>VEC; Rect rect; Rect.id=1; Rect.length=2; Rect.width=3; Vec.push_back (rect); Vector<rect>::iterator it=Vec.begin (); cout<< (*it) .id<<' '<< (*it) .length<<' '<< (*it) .width<<Endl; return 0;}
3. Algorithms
(1) Flip the element with reverse: Requires a header file #include<algorithm>
Reverse (Vec.begin (), Vec.end ()); Flips the element (in the vector, if two iterators are required in a function, the latter does not usually contain.)
(2) Sorting by using sort: Requires header file #include<algorithm>
Sort (Vec.begin (), Vec.end ());(by default in ascending order, that is, from small to large).
You can compare functions in descending order by overriding the sort comparison, as follows:
To define a sort comparison function:
BOOL Comp (const int &a,const int &b) {
Return a>b;
}
4. Summary
#include <stdio.h>#include<algorithm>#include<iostream>#include<vector>using namespacestd;BOOLCompConst int&a,Const int&b) { returna>b;//Descending, A>b returns True when the order of exchange is true}voidmain () {vector<int>VEC; Vec.push_back (5); cout<<"1st element:"<<vec[0]<<Endl; Vec.push_back (2); Vec.push_back (5); Vec.push_back (8); Vec.push_back (1); Vec.push_back (7); Vector<int>:: Iterator it; cout<<"After push:"<<Endl; for(It=vec.begin (); It!=vec.end (); it++) {cout<<"element:"<<*it<<Endl; } sort (Vec.begin (), Vec.end ()); //sort (Vec.begin (), Vec.end (), comp);cout<<"After sort:"<<Endl; for(It=vec.begin (); It!=vec.end (); it++) {cout<<"element:"<<*it<<Endl; } reverse (Vec.begin (), Vec.end ()); cout<<"After reverse"<<Endl; for(It=vec.begin (); It!=vec.end (); it++) {cout<<"element:"<<*it<<Endl; } vec.insert (Vec.begin ()+2, A);//Insert in this position, all elements move backwardscout<<"After insert:"<<Endl; cout<<"3th element:"<<vec[2]<<Endl; cout<<"4th element:"<<vec[3]<<Endl; Vec.erase (Vec.begin ()+2); cout<<"After erase:"<<Endl; cout<<"Vector Size:"<<vec.size () <<Endl; Vec.clear (); cout<<"After clear:"<<Endl; cout<<"Vector Size:"<<vec.size () <<Endl; System ("Pause");}
Vectors in C + +