Vector is a random access array type, which provides fast and random access to array elements, as well as fast and random insertion and deletion operations at the end of the sequence. It is a variable size vector that can be changed when needed.
1. Vector objects need to be created to use vector class templates. There are several methods to create vector objects.
(1),std::vector<type>name;
This method creates an empty vector object named name, which can hold data of type.
(2),std::vector<type>name(size)
This method is used to initialize the vector object with the number of size elements
(3),std::vector<type>name(size,value)
This method is used to initialize the vector object with the number of size elements and set the initial value of the object to value
(4),std::vector<type>name(myvector)
This method uses the copy constructor to create a vector object with the existing vector myvector
(5),std::vector<type>name(first,last)
This method creates a vector of elements within the specified range. first represents the start range and last represents the end range
2. Main member functions and descriptions of vector object
assign(first,last) Replace vector elements with elements within the scope of iterators first and last
assign(num.value) Replace the vector element with num copies of val
at(n) Returns the value of the nth position element in a vector
back Returns a reference to the element at the end of a vector
begin Returns the iterator of the first element in the specified vector
capcity Returns the maximum number of elements that the current vector can hold
clear Deletes all elements in the vector
empty Returns a true value if the vector is empty
end Returns an iterator pointing to the last vector in a vector
erase(start,end) Deletes the vector elements in the iterator start to end range
erase(i) Delete the vector element pointed to by iterator I
front Returns a reference to a vector element
insert(i,x) Inserts the value x into the vector at the position indicated by the iterator I
insert(i,start,end) Insert the elements in the range of iterators start to end into the vector at the position indicated by iterator I
max_size Returns the maximum capacity of the vector
pop_back Deletes the last element of the vector
push_back(x) Put the value x at the end of the vector
rbegin Returns a reverse iterator pointing after the last element of the vector
rend Returns a reverse iterator that points to the vector starting element
reverse Reverse the order of elements
resize(n,x) Reset the vector size n and initialize the value of the new element to X
size Returns the size of the vector
swap(vector) Swap the contents of two vectors
3. Instance
#include<iostream> #include<vector> using std::cout; using std::endl; using std::vector; void main() { vector<int>v1,v2; v1.reserve(10); v2.reserve(10); v1=vector<int>(8,7); int array[8]={1,2,3,4,5,6,7,8}; v2=vector<int>(array,array+8); cout<<"v1 capacity"<<v1.capacity()<<endl; cout<<"v1 Current items:"<<endl; for(decltype(v2.size())i=0;i<v1.size();i++) { cout<<" "<<v1[i]; } cout<<endl; cout<<"v2 capacity"<<v2.capacity()<<endl; cout<<"v2 Current items:"<<endl; for(vector<int>::size_type i=0;i<v1.size();i++) { cout<<" "<<v2[i]; } cout<<endl; v1.resize(0); cout<<"v1 Capacity through resize The function becomes 0"<<endl; if(!v1.empty()) cout<<"v1 capacity"<<v1.capacity()<<endl; else cout<<"v1 It's empty"<<endl; cout<<"take v1 Capacity expanded to 8"<<endl; v1.resize(8); cout<<"v1 Current items"<<endl; for(decltype(v1.size())i=0;i<v1.size();i++) { cout<<" "<<v1[i]; } cout<<endl; v1.swap(v2); cout<<"v1 And v2swap Yes"<<endl; cout<<"v1 capacity"<<v1.capacity()<<endl; cout<<"v1 Current items:"<<endl; for(decltype(v2.size())i=0;i<v1.size();i++) { cout<<" "<<v1[i]; } cout<<endl; v1.push_back(3); cout<<"from v1 Element 3 is added later"<<endl; cout<<"v1 capacity"<<v1.capacity()<<endl; cout<<"v1 Current items:"<<endl; for(decltype(v2.size())i=0;i<v1.size();i++) { cout<<" "<<v1[i]; } cout<<endl; v1.erase(v1.end()-2); cout<<"The penultimate element was deleted"<<endl; cout<<"v1 capacity"<<v1.capacity()<<endl; cout<<"v1 Current items:"<<endl; for(decltype(v2.size())i=0;i<v1.size();i++) { cout<<" "<<v1[i]; } cout<<endl; v1.pop_back(); cout<<"v1 Through stack operation pop_back Let go of the last element"<<endl; cout<<"v1 capacity"<<v1.capacity()<<endl; cout<<"v1 Current items:"<<endl; for(decltype(v2.size())i=0;i<v1.size();i++) { cout<<" "<<v1[i]; } cout<<endl; }
give the result as follows
In this example, both v1 and v2 allocate space with resize. When the allocated space is smaller than its original size, the original tail element is deleted. When the allocated space is larger than its own space, the 0 value of the corresponding number is automatically added after the end element.