The template class vector is a sequential container that encapsulates an array of dynamic sizes, and the sequences it controls are stored in a contiguous array. As with any other type of container, it can hold various types of objects.
Include header file # include <vector> declaration namespace using namespace Std;
Characteristics:
1) Sequential sequence: The elements in the container are sorted in strict linear order. The corresponding element can be accessed by the element's position in the sequence.
2) Dynamic array: Enables quick and direct access to any element in the sequence. Operation to add/remove elements relatively quickly at the end of a sequence.
3) Ability to sense the memory allocator (allocator-aware): The container uses a memory allocator object to dynamically process its storage requirements.
How to use:
1) constructor function
Vector (); Create an empty vector such as:vector<int> A;
Vector (int nSize); Create an empty vector with the number of elements nsize such as:vector<int> A (10);
Vector (int nsize,const t& val); Create a vector with a number of elements of nsize and a value of Val such as:vector<string> a (5, "has fun");
Vector (const vector&); Copy (copy) constructor
such as:vector<string> A (5, "has fun");
Vector<string> B (a);
Vector (begin,end); Copy elements from another array [begin,end] range into the vector
such as:intia[6] = { -2, -1, 0, 1, 2, 1024 };
vector<int
> a(ia,ia+6);
2) interpolation function
void push_back (const t& x); Adds an element x to the tail of the vector
such as: Vector<string> A (5, "has fun");
A.push_back ("Good luck");
for (int i=0;i<a.size (); i++)
{
cout<<a[i]<<endl;
}
Program output:
Iterator Insert (iterator it,const t& x); Adds an element x before an iterator to an element in a vector
such as: Vector<string> A (5, "has fun");
A.insert (A.begin () +2, "Good luck");
for (int i=0;i<a.size (); i++)
{
cout<<a[i]<<endl;
}
Program output:
Iterator Insert (iterator it,int n,const t& x); Adds n identical elements to an iterator in the vector before it points to the element x
such as: Vector<string> A (5, "has fun");
A.insert (A.begin () +2,2, "Good luck");
for (int i=0;i<a.size (); i++)
{
cout<<a[i]<<endl;
}
Program output:
Iterator Insert (iterator it,const_iterator first,const_iterator last); The data between an iterator in a vector and a [first,last] that inserts another vector of the same type before it points to the element
such as: Vector<string> A (5, "has fun");
Vector<string> B (2, "good luck");
A.insert (A.begin () +2,b.begin (), B.end ());
for (int i=0;i<a.size (); i++)
{
cout<<a[i]<<endl;
}
Program output:
3) Delete function
Iterator Erase (iterator it); The iterator in the delete vector points to the element;
such as: string str[7] = {"Has fun", "has fun", "Good Luck", "Good Luck", "has Fun", "has Fun", "has Fun"};
Vector<string> A (str,str+7);
A.erase (A.begin () +2);
for (int i=0;i<a.size (); i++)
{
cout<<a[i]<<endl;
}
Program output:
Iterator Erase (iterator first,iterator last); Delete the element in [First,last] in the vector, note that last is the next element that points to the final element to be deleted;
such as: string str[7] = {"Has fun", "has fun", "Good Luck", "Good Luck", "has Fun", "has Fun", "has Fun"};
Vector<string> A (str,str+7);
A.erase (A.begin () +2,a.begin () +4);
for (int i=0;i<a.size (); i++)
{
cout<<a[i]<<endl;
}
Program output:
void Pop_back (); Deletes the last element in a vector;
such as: string str[7] = {"Has fun", "has fun", "Good Luck", "Good Luck", "has Fun", "has Fun", "has Fun"};
Vector<string> A (str,str+7);
A.pop_back ();
for (int i=0;i<a.size (); i++)
{
cout<<a[i]<<endl;
}
Program output:
void Clear (); Empties all elements in the vector, functionally equivalent to erase (Vector.begin (), Vector.end ());
4) Traversal function
Reference at (int pos); Returns a reference to the POS location element
Reference Front (); Returns a reference to the first element
Reference back (); Returns a reference to the tail element
Iterator begin (); Returns the vector head pointer, pointing to the first element
Iterator End (); Returns the vector tail pointer, pointing to the next position of the last element of the vector
Reverse_iterator Rbegin (); Reverse iterator, pointing to the last element
Reverse_iterator rend (); A reverse iterator that points to the position before the first element
5) Judgment function
BOOL Empty () Const: Determines whether the vector is empty and returns true if NULL
6) Size function
void Resize (size_type n); Adjust the elements of the vector to N, multiple deletions, and a random value
void Resize (size_type n,t& x); Adjust the elements of the vector to N, multiple deletions, and a value of X
void Reserve (Size_type n); Set the storage space allocated to vectors to N, note that you cannot exceed max_size
int size () const; Returns the number of elements in the current vector
int capacity () const; Returns the size of the storage space currently assigned to the vector
int max_size () const; Returns the maximum allowable vector element number value
such as: string str[7] = {"Has fun", "has fun", "Good Luck", "Good Luck", "has Fun", "has Fun", "has Fun"};
Vector<string> A (str,str+7);
A.pop_back ();
Cout<<a.size () <<endl;
Cout<<a.capacity () <<endl;
Cout<<a.max_size () <<endl;
Program output:
7) Other functions
void swap (vector&); Exchange data for two vectors of the same type
void assign (int n,const t& x); Sets the value of the nth element in a vector to X
void Assign (Const_iterator first,const_iterator last); Sets the element in the vector [First,last] to the current vector element
8) Common algorithms
Sort by: Sort (A.begin (), A.end ()); The elements of sequence [begin,end] in vector A are arranged from small to large
Inversion: Reverse (A.begin (), A.end ()); The elements of the sequence [begin,end] in vector A are inverted, but not arranged, such as the element in A is 1,3,2,4 and inverted to 4,2,3,1
Copy: Copy (A.begin (), A.end (), B.begin () +1); Copies the elements of the sequence [begin,end] in vector A into B, copying from the position of the B.begin () +1, overwriting the original elements
Find: Find (A.begin (), A.end (), 10); Find 10 in the elements of sequence [begin,end] in vector A, if there is a position to return it in the vector
STL Standard Library of C + + learning notes (iii) vector container