[C++] STL vector container (definition and basic function application)

table of Contents

1. What is a vector?

Second, the characteristics of the container

1. Sequential sequence

2. Dynamic array

3. Able to perceive the memory allocator (Allocator-aware)

Three, basic function realization

1. Constructor

2. Increase the function

3. Delete function

4. Traverse function

5. Judgment function

6. Size function

7. Other functions

Four, example demonstration

1. Definition and access of one-dimensional arrays (direct array access & iterator access)

2.The pop_back()&push_back(elem) instance removes and inserts data at the end of the container

3.insert()&erase(elem) instance inserts and removes data anywhere in the container

4.clear() clear all data in the container

5. Sort function sort() and reverse order function reverse()

6. Definition of two-dimensional array


1. What is a vector?

Vector is a sequence container (Sequence Container) that encapsulates a dynamically sized array. Like any other type of container, it can store various types of objects. It can be simply considered that a vector is a dynamic array that can store any type.


Second, the characteristics of the container

1. Sequential sequence

The elements in the sequence container are sorted in strict linear order. The corresponding element can be accessed by its position in the sequence.

2. Dynamic array

It supports quick and direct access to any element in the sequence, and even this operation can be performed through pointer calculation. It provides operations for adding/removing elements relatively quickly at the end of the sequence.

3. Able to perceive the memory allocator (Allocator-aware)

The container uses a memory allocator object to dynamically handle its storage requirements.


Three, basic function realization

1. Constructor

  • vector(): create an empty vector
  • vector(int nSize): Create a vector, the number of elements is nSize
  • vector(int nSize,const t& t): create a vector, the number of elements is nSize, and the value is t
  • vector(const vector&): copy constructor
  • vector(begin,end): copy the elements of another array in the range of (begin,end) to the vector

2. Increase the function

  • void push_back(const T& x): add an element X to the end of the vector
  • iterator insert(iterator it,const T& x): Add an element x before the iterator points to the element in the vector
  • iterator insert(iterator it, int n, const T& x): Add n identical elements x before the iterator points to the element in the vector
  • iterator insert (iterator it, const_iterator first, const_iterator last): insert the data between (first, last) of another vector of the same type before the iterator points to the element in the vector

3. Delete function

  • iterator erase(iterator it): delete the element pointed to by the iterator in the vector
  • iterator erase(iterator first, iterator last): delete elements in (first, last) in the vector
  • void pop_back(): delete the last element in the vector
  • void clear(): Clear all elements in the vector

4. Traverse function

  • reference at(int pos): returns the reference of the element at pos
  • reference front(): Returns the reference of the first element
  • reference back(): Returns the reference of 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(): reverse iterator, pointing to the position before the first element

5. Judgment function

  • bool empty() const: Determine whether the vector is empty, if it is empty, there are no elements in the vector

6. Size function

  • int size() const: returns the number of elements in the vector
  • int capacity() const: returns the maximum element value that the current vector can hold
  • int max_size() const: returns the maximum allowable number of vector elements

7. Other functions

  • void swap(vector&): Exchange the data of two vectors of the same type
  • void assign(int n,const T& x): Set the value of the nth element in the vector to x
  • void assign(const_iterator first, const_iterator last): The element in the vector (first, last) is set to the current vector element

Four, example demonstration

1. Definition and access of one-dimensional arrays (direct array access & iterator access)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int>obj1;//创建一个向量存储容器 int
    vector<int>obj2(20,1);//创建一个int型向量容器,最大容量20,初始值为0
    for (int i = 0; i < 10; i++) // push_back(elem)在数组最后添加数据 
    {
        obj1.push_back(i+1);
    }

    //直接数组访问
    cout << "数组1:";
    for (int i = 0; i < obj1.size(); i++)//size()容器中实际数据个数 
    {
        cout << obj1[i] << " ";
    }
    cout << endl;

    //迭代器访问
    cout << "数组1:";
    vector<int> ::iterator it;//声明一个迭代器
    for (it = obj1.begin(); it != obj1.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;

    vector<int> obj3(obj1);
    for (int i = 0; i < 5; i++)//去掉数组最后一个数据 
    {
        obj3.pop_back();
    }

    cout << "数组3:";
    for (int i = 0; i < obj3.size(); i++)//size()容器中实际数据个数 
    {
        cout << obj3[i] << " ";
    }
    cout << endl;
 
    int a[] = { 1,2,3,4 };
    vector<int> obj4(a,a+4);
    cout << "数组4:";
    for (int i = 0; i < obj4.size(); i++)//size()容器中实际数据个数 
    {
        cout << obj4[i] << " ";
    }
    cout << endl;
    return 0;
}

operation result:

2.The pop_back()&push_back(elem) instance removes and inserts data at the end of the container

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int>obj1;//创建一个向量存储容器 int
    for (int i = 0; i < 10; i++) // push_back(elem)在数组最后添加数据 
    {
        obj1.push_back(i + 1);
    }

    cout << "原始数组1:";
    for (int i = 0; i < obj1.size(); i++)//size()容器中实际数据个数 
    {
        cout << obj1[i] << " ";
    }
    cout << endl;

    
    for (int i = 0; i < 5; i++)//去掉数组最后一个数据 
    {
        obj1.pop_back();
    }

    cout << "删除后数组1:";
    vector<int> ::iterator it;//声明一个迭代器
    for (it = obj1.begin(); it != obj1.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;

  
    return 0;
}

operation result:

3.insert()&erase(elem) instance inserts and removes data anywhere in the container

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int>obj1;//创建一个向量存储容器 int
    for (int i = 0; i < 10; i++) // push_back(elem)在数组最后添加数据 
    {
        obj1.push_back(i + 1);
    }

    cout << "原始数组:";
    for (int i = 0; i < obj1.size(); i++)//size()容器中实际数据个数 
    {
        cout << obj1[i] << " ";
    }
    cout << endl;


    obj1.erase(obj1.begin()+1);//删除数组第二个数据 

    cout << "删除数组第二个数据后:";
    vector<int> ::iterator it;//声明一个迭代器
    for (it = obj1.begin(); it != obj1.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;

    obj1.erase(obj1.begin() + 2, obj1.begin() + 5);//删除数组中第三到第五个数据(左闭右开)
    cout << "删除数组中第三到第五个数据后:";
    for (it = obj1.begin(); it != obj1.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;

    obj1.insert(obj1.end() - 2,0);//倒数第三个位置插入0
    cout << "倒数第三个位置插入0后:";
    for (it = obj1.begin(); it != obj1.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

operation result:

4.clear() clear all data in the container

#include <vector>
#include <iostream>
using namespace std;
 
int main()
{
    vector<int>obj;
    for(int i=0;i<10;i++)//push_back(elem)在数组最后添加数据 
    {
        obj.push_back(i);
        cout<<obj[i]<<",";
    }
 
    obj.clear();//清除容器中所以数据
    for(int i=0;i<obj.size();i++)
    {
        cout<<obj[i]<<endl;
    }
 
    return 0;
}

operation result:

5. Sort function sort() and reverse order function reverse()

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int>obj1;//创建一个向量存储容器 int
    obj1.push_back(-2);
    obj1.push_back(1);
    obj1.push_back(13);
    obj1.push_back(23);
    obj1.push_back(0);
    obj1.push_back(96);
    obj1.push_back(11);

    cout << "原始数组:";
    for (int i = 0; i < obj1.size(); i++)//size()容器中实际数据个数 
    {
        cout << obj1[i] << " ";
    }
    cout << endl;

    sort(obj1.begin(), obj1.end());//默认为升序排序
    cout << "升序排序数组:";
    vector<int> ::iterator it;//声明一个迭代器
    for (it = obj1.begin(); it != obj1.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;

   //逆序方法1
    reverse(obj1.begin(), obj1.end());
    cout << "逆序数组1:";
    for (int i = 0; i < obj1.size(); i++)//size()容器中实际数据个数 
    {
        cout << obj1[i] << " ";
    }
    cout << endl;

    int obj2[] = { 1,2,3,4,5 };
    int len = sizeof(obj2) / sizeof(obj2[0]);

    cout << "原始数组2:";
    for (int i = 0; i <len; i++)//size()容器中实际数据个数 
    {
        cout << obj2[i] << " ";
    }
    cout << endl;
    //逆序方法2
    sort(obj2, obj2+len,greater<int>());
    cout << "逆序数组2:";
    for (int i = 0; i < len; i++)//size()容器中实际数据个数 
    {
        cout << obj2[i] << " ";
    }
    cout << endl;
    return 0;
}

operation result:

6. Definition of two-dimensional array

/*方法一*/
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

int main()
{
    int N = 5, M = 6;
    vector<vector<int> > obj(N); //定义二维动态数组大小5行 
    for (int i = 0; i < obj.size(); i++)//动态二维数组为5行6列,值全为0 
    {
        obj[i].resize(M);
    }

    for (int i = 0; i < obj.size(); i++)//输出二维动态数组 
    {
        for (int j = 0; j < obj[i].size(); j++)
        {
            obj[i][j] = M*i + j+1;
            cout << setw(3)<< obj[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

operation result:

/*方法二*/
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;


int main()
{
    int N = 5, M = 6;
    vector<vector<int> > obj(N,vector<int>(M)); //定义动态二维数组为5行6列,值全为0 
  

    for (int i = 0; i < obj.size(); i++)//输出二维动态数组 
    {
        for (int j = 0; j < obj[i].size(); j++)
        {
            obj[i][j] = M * i + j + 1;
            cout << setw(3) << obj[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

operation result:

Guess you like

Origin blog.csdn.net/Jacky_Feng/article/details/109114318