Implementation of C++ Vector dynamic array

  • 2020-06-23 01:34:56
  • OfStack

Introduction to the

A vector (Vector) is a sequential container that encapsulates a dynamic size array. A vector is a dynamic array that can hold any type.

Use of C++ in Vector

The header file # include < vector > You need to use the std namespace using namespace std; The following usage methods take int data types as an example, which can be customized when used Note: the middle interval below is left closed and right open

1. Define (initialize) Vector

vector < int > v; Create 1 empty vector vector < int > v (5); Create 1 vector with 5 elements vector < int > v (5); Create an vector with 5 elements and a value of 10 for each element vector < int > v2 (v1); Copy another vector to make v2 equal to v1 vector < int > v (begin end); Copy the elements of the other array in the interval [begin,end] to vector

int a[]={2,4,6,8,10};
vector<int> v(&a[1],&a[3]);

2. Add elements to Vector

v.push_back(x) adds an element x to the tail v.insert(pos,x) adds an element x before the pos address points to the element

v.insert(v.begin(),666); // Insert the element before the first element  666
v.insert(v.begin()+1,666); // In the first 2 Insert element before element  666

v.insert(pos,n,x) adds n to the same element before the pos address points to the element


v.insert(v.begin(),3,666); // Insert before the first element  3  An element  666

v (pos,first,last) inserts data between another vector of the same type [first,last] before the pos address points to the element


v.insert(v.begin(),v2.begin(),v2.end()); // will v2 All element insertion v1 before 

Remove elements from Vector

v. pop_back() deletes the last element in the vector v.clear() clears all elements in the vector v.erase(pos) removes the iterator pointing element from the vector

v.erase(v.begin()); // Delete header element 

v.erase(first,last): Removes elements from the vector [first,last]


v.erase(v.begin()+1,v.end()-1); // Delete the first 2 One to the last 2 Student: between the elements 

4. Iterate over the elements in Vector

v[i] accesses elements in Vector directly v. at(pos) returns the value of the pos position element with the pos subscript starting at 0 (similar to an array) v.front () returns the value of the first element v.back () returns the value of the tail element v. begin() returns a vector header pointer to the first element v.end () returns a pointer to the tail of the vector, pointing to the next position of the last element of the vector v. rbegin() reverse iterator, pointing to the last element v. rend() reverse iterator, pointing to the position before the first element

// Traverse directly through the element 
for(int i=0;i<v.size();i++)
{
 cout<<v[i]<<" ";
}

// Iterators are used to traverse elements 
for(vector<int>::iterator i=v.begin();i<v.end();i++)
{
 cout<<*i<<" ";
}

// Traversing elements using a reverse iterator (reverse output) 
for(vector<int>::reverse_iterator i=v.rbegin();i<v.rend();i++)
{
 cout<<*i<<" ";
}

5. Other common methods

v. empty() determines whether the vector is null, returns 1 for null or 0 for null v.size() returns the number of elements in the vector v. capacity() returns the maximum number of elements that the current vector can hold v.max_size () returns the maximum allowable number of vector elements v.swap(v2) swaps two vectors of the same type v and v2 v (n,x) sets the value of the n element in the vector to x v.assign(first,last) sets the [first,last) element in the vector to the current vector element

// will v Element is set to v2 Elements in the 
v.assign(v2.begin(),v2.end());

Summary 1 common syntax

语法 说明
vector<int> v; 创建空vector
v.push_back(x) 向尾部增加1个元素 x
v.insert(pos,x) 向pos地址指向元素前增加1个元素 x
v[i] 访问 i 位置元素
v.pop_back() 删除向量中最后1个元素
v.clear() 清空向量中所有元素
v.empty() 判断向量是否为空
v.size() 返回向量中元素的个数
v.begin() 返回向量头指针(迭代器),指向第1个元素
v.end() 返回向量尾指针(迭代器),指向最后1个元素+1位置


Related articles: