Vector of STL

Source: Internet
Author: User

1.vector is a dynamic array that supports fast random access, so that elements are stored continuously, and when there is no space to accommodate the new elements, the container must allocate new memory space to move the previous elements to the new space and then add new elements to free the old space. Vector performance is very poor if each new element is added, and in order to improve performance, vectors usually allocate a reserved space to accommodate the new element when allocating memory space, so that it does not have to reallocate memory every time. This leads to two important data of vectors. Size and capacity:size represent vector sizes (the actual number of elements), and capacity represents how much space the vector actually allocates in the amount of memory space that is not reallocated (the number of elements that can hold the element), and how the compiler implements it. (V.size () and v.capacity ())

1#include <iostream>2#include <vector>3 using namespacestd;4 int_tmain (intARGC, _tchar*argv[])5 {6vector<int>v;7      for(inti =0;i<Ten;++i)8     {9 V.push_back (i);Tencout <<"Size:"<< v.size () <<"Capacity:"<< v.capacity () <<Endl; One     } A     return 0; -}

2.vector initialization

Vector<t> v1; Empty vector

Vector<t> v2 (v1);//v2 contains all the elements of V1

Vector<t> v2=v1;//One Way

Vector<t> v3 (n,val);//v3 contains n val

Vector<t> v4 (n);//v4 contains n data, the data is constructed by default.

Vector<t> V5{A,B,C};//V5 contains a,b,c three elements (list initialization mode, C++11 assignment new method, VS2012 not supported)

Vector<t> v5={a,b,c};//the same way

Vetor<t> V6 (V1.begin (), V1.end ())//begin,end mode

1#include <iostream>2#include <vector>3 using namespacestd;4 int_tmain (intARGC, _tchar*argv[])5 {6vector<int>v1;7      for(inti =0;i<Ten;++i)8     {9 V1.push_back (i);Ten     } One      for(inti:v1) { Acout<<i<<"  "; -     } -cout<<Endl; thevector<int>v2 (v1); -      for(inti:v2) { -cout<<i<<"  "; -     } +cout<<Endl; -vector<int> v3=v1; +      for(inti:v3) { Acout<<i<<"  "; at     } -cout<<Endl; -vector<int> v4 (Ten,5); -      for(inti:v4) { -cout<<i<<"  "; -     } incout<<Endl; -vector<int> V5 (Ten); to      for(inti:v5) { +cout<<i<<"  "; -     } thecout<<Endl; *vector<int> V6 (V1.begin () +1, V1.end ()-2); $      for(inti:v6) {Panax Notoginsengcout<<i<<"  "; -     } thecout<<Endl; +     return 0; A}

3. Adding elements and subscript operations (V.push_back ())

Vector adds element V.push_back (val) to the tail end;

It is not possible to add elements here in the following way, which is wrong.

1 vector<int> v; 2      for (int i=0; i!=; + +i) {3         v[i]<<=i; 4     }

And although the space reserved by the vector is larger than the known space, it is only possible to subscript the known space
4.vector traversal (three types, which are simple for loops, iterators Traverse and take advantage of v.size () General for Loop V.begin () and V.end ())

1#include <iostream>2#include <vector>3 using namespacestd;4 int_tmain (intARGC, _tchar*argv[])5 {6vector<int>v;7      for(intI=1; i!=Ten;++i) {8 V.push_back (i);9     }Ten      for(inti:v) { Onecout<<i<<"  "; A     } -cout<<Endl; -      for(vector<int>::iterator It=v.begin (); It!=v.end (); + +it) { thecout<<*it<<"  "; -     } -cout<<Endl; -      for(inti =0; I!=v.size (); + +i) { +cout<<v[i]<<"  "; -     } +cout<<Endl; A     return 0; at}

5.vector Other operations

V.assign (Begin,end)//assigns the data in the [begin; end] Interval to V.

V.assign (N,elem)//assigns a copy of n elem to V.

v.at (ID)//returns the data referred to by the index ID, if the ID is out of bounds, throws Out_of_range.

V.clear ()//Removes all data from the container. (Memory still in)

V.~vector<t> ()//Destroy data, free memory

V.empty ()//Determine if the container is empty.

V.insert (Pos,elem)//Insert a elem copy at the POS location to return the new data location (location refers to the address value returned).

V.insert (Pos,n,elem)//inserts n elem data at POS location. no return value.

V.insert (Pos,begin,end)//The data in the [Begin,end] interval is inserted at the POS location. no return value.

V.erase (POS)//Deletes the POS location data and returns the location of the next data.

V.erase (begin,end)//delete the data from the [Begin,end] interval, passing back the position of the next data.

V.rbegin ()//returns the first data for a reverse queue.

V.rend ()//returns the next position of the last data in a reverse queue.

Compare V.begin () and V.end () to illustrate that using V.rbegin () and v.rend can reverse traverse

1vector<int> V (Ten);2      for(inti =0; I <Ten; i++)3V[i] = i +1;4      for(vector<int>::reverse_iterator rit = V.rbegin (); Rit! = V.rend (); rit++)5cout << *rit <<" ";6cout << Endl;

V.pop_back ()//delete the last data.

V.front ()//returns the first data.

V.back ()//pass back the last data

Front () and back () can be used as lvalue, stating that the return value of the function returns a reference and can be modified.

V1.swap (v2)//Interchange V1 and V2 elements.

Swap (V1,V2)//ditto operation.

V.resize (n)//Adjust the number of elements of V is n, if N<v.size (), the extra elements will be discarded, but the pre-allocated memory space will not be changed.

V.resize (N,elem)//adjust V for the number of elements n, no element is added when initialized to Elem

V.reserve (n)//allocates n element space, which does not affect the number of elements in a container. If N<v.capacity () does nothing, it does not return memory space. Thus, this function does not reduce the container memory space, nor discards the element.

Thus, the capacity will only increase when the memory space is insufficient, without decreasing.

Vector of STL

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.