Vector of C++ STL (Basic Part 1)

Q1: What is vector?

Answer: A vector is a container, and a container is used to manage a collection of objects of a certain type . C++ provides various types of containers, such as deque, list, vector , map, etc. vector (vector), is avariable length array, that is, an array that automatically changes the length of the array (the length of the array is determined when it is defined, and it cannot be changed, while the length of the vector container is not fixed and can be changed at any time )

Q2: How to use vector?

1. Add the header file

When using vector, it needs to be added #include <vector>before it can be used normally ( this step is very important!! )

2. The definition of vector

vector<类型名> 变量名;
The type name can be int, double, char, struct, or STL container: vector, set, queue

vector<int> name;
vector<double> name;
vector<char> name;
vector<struct node> name;
vector<vector<int> > name;//注意:> >之间要加空格
  • A vector array is a one-dimensional array
    (for vector<int> name;example, name is equivalent to the name of a one-dimensional array storing integer variables)
  • If it is defined as an array of vector arrays , it is a two-dimensional array
    (such as vector<int> array[SZIE]; //二维变长数组)
    summary:Low-dimensional is the address of high-dimensional(For example: in a two-dimensional array, its one-dimensional form is the address )

3. Vector initialization

  • Method 1 (direct method)
vector<int> v1={
    
    1,2,3,4,5};

At this time, v1 is equivalent to a one-dimensional array containing 5 elements
Note: Some version compilers do not support such initialization

  • Method 2 (array pointer method)
   int a[5]={
    
    1,2,3,4,5};//先定义一个数组
   vector<int> v1(a,a+5);//注意末尾指针指向最后一个元素之后的位置
   //下面为遍历vector容器,输出结果,检测初始化结果是否正确
   for(int i=0;i<v1.size();i++) //v1.size()为获取容器的大小
       cout<<v1[i]<<' ';
   cout<<endl;
   **结果:1 2 3 4 5
  • Method 3 (n elems)
vector<int> v2(3,6);//将三个6赋值给v2
    for(int i=0;i<v2.size();i++)
        cout<<v2[i]<<' ';
    cout<<endl;
    **结果:6 6 6
  • Method 4 (copy method)
vector<int> v3(v1);//将vector容器v1作为参数传给v3,则v3容器中的数据和v1中相同
    for(int i=0;i<v3.size();i++)
        cout<<v3[i]<<' ';
    cout<<endl;
    **结果:1 2 3 4 5 

Note: The v1 passed in as a parameter can only bevector type, cannot be an array name

  • Method 5:Use the assign method
//方法1(数组指针)
    vector<int> v4,v5,v6,v7;
    v4.assign(a,a+5);
    for(int i=0;i<v4.size();i++)
        cout<<v4[i]<<' ';
    cout<<endl;
    //方法2()
    v5.assign(4,10);
    for (int i = 0; i < v5.size(); ++i)
        cout<<v5[i]<<' ';
    cout<<endl;
    //方法3(容器赋值容器)
    v6.assign(v1.begin(),v1.end());
    for (int i = 0; i < v6.size(); ++i)
        cout<<v6[i]<<' ';
    cout<<endl;
    **结果:1 2 3 4 5 
           10 10 10 10 
           1 2 3 4 5 

**Replenish:**Take interval element assignment
For example, there is a number vector container v1 and v1={1,2,3,4,5}I just want to {2,3}assign it to vector container v2, then we canchange the pointing of the pointerto change the storage range as follows:

    v7.assign(v1.begin()+1,v1.end()-2);
    for (int i = 0; i < v7.size(); ++i) {
    
    
        cout<<v7[i]<<' ';
    }
    **结果:2 3

Note: the end pointer points to the next position of the end element ( for example, the end pointer here points to element 4 )

4. The output of the vector container element

  • One is to access data through subscript indexing, which is similar to arrays. If there is a vector container v1, if you want to access the first element of v1, you can access it through v1[0] , and so on. The above codes are all subscript indexing methods
  • The second use at()access, see examples:
    vector<int> v1;
    v1={
    
    1,2,3,4,5};
    for (int i = 0; i < v1.size(); ++i) {
    
    
        cout<<v1.at(i)<<' ';
    }
    cout<<endl;
    **结果:1 2 3 4 5 

expand: So what is the difference between these two access methods?
Vector is a dynamic array. Since it is an array, there is a possibility of crossing the boundary . Let's look at an example:

  1. The subscript index method encounters an array out-of-bounds access situation
vector<int> v1;
   v1={
    
    1,2,3,4,5};
   cout<<v1[6]<<endl;
   **结果:-1038988976
  1. The at() method encounters an array out-of-bounds access situation
vector<int> v1;
   v1={
    
    1,2,3,4,5};
   cout<<v1.at(6)<<endl;
   **结果:terminate called after throwing an instance of 'std::out_of_range'

Here we can see that the subscript method will not report an error when the array is out of bounds , but output garbled characters, but at()the method will report the wrong type , so at()the method is more conducive to finding code errors

Vector of C++STL (Basic part 2)

Guess you like

Origin blog.csdn.net/weixin_74334323/article/details/130171281