1. Header files
#include <vector>
2. Array idiom
vector<int> IVec (Ten);//define 10 integersivec[0] =Ten;//the first element is assigned a value of tenvector<int> IVec (Ten, -1);//defines 10 integers, with each integer initialized to 1intia[6] = { -2, -1,0,1,2,1024x768};vector<int> IVec (IA, ia+6);//Copy the 6 elements of IA into the IvecVector<int> Ivec (&ia[2], &ia[5] );//copy 3 elements ia[2], ia[3], ia[4]
3. STL Idioms
1) Insert element at tail, push_back
string Word; while (cin>>Word) {text.push_back (word); ...}
Although you can also access them as follows,
" words read is: \ n " forint0; IX < Text.size (); + +IX )';
However, the classic approach is to use the iterator iterator returned by the Begin () and end () in the vector operations set.
cout<<"words read are:\n"; for (vector<string>::iterator it = Text.begin (); It! = Text.end (); + + it) { cout<<*it< <""; } cout<<endl;
Where iterator is a class in the standard library, it has the function of pointers, and *it is a dereference to an iterator and accesses the actual object it points to, ++it moves forward, making it point to the next element
2) It is not possible to mix these two idioms
vector<int> ivec; ivec[01024x768// wrong, because Ivec has not the first element, we can only index The element that already exists in the vector size () operation returns the number of elements contained in the vector
3) Similarly, when we specify a vector for a given size, the definition is as follows
vector<int );
4. Vector Common operation
#include <vector>//header FileVector<int> VEC;//creating a Vector objectVec.push_back (a);//Trailing Insert Numbercout<<vec[0]<<endl;//use subscript to access elements, 0 startVector<int>::iterator it;//using iterators to access elements for(it = Vec.begin (); It! = Vec.end (); it + +) cout<<*it<<Endl;vec.insert (Vec.begin ()+I,A);//insert a in front of the first + 1 elementsvec.erase (Vec.begin ()+2);//Remove 3rd ElementVec.erase (Vec.begin () +i,vec.end () +j);//delete interval [i,j-1]; interval starting from 0vec.size ();//Vector sizevec.clear ();//Clear
Need 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 is not normally included.)sort (Vec.begin (), Vec.end ());//(the default is in ascending order, that is, from small to large). You can compare by overriding the sort comparison function in descending order
stl--Vector