STL-Vector

vector is a dynamic array

When we want to use vector,we should write this #include

We can add and delete element at the tail of vector by using function push_back and pop_back.

I write a demo, let us see it.

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

int main()
{
    vector<int> c;  //Create an empty vector
    for (int i = 0; i <= 8; ++i)
    {
        c.push_back(i);  //Add some elements in vector
    }

    for (int j : c)  
        cout << j << ' ';
    cout << endl;

    for (int j = 0; j < c.size(); ++j)
    {
        cout << c[j] << ' ';
    }
    cout << endl;
}

Output:
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8

Basic operation

//create a vector
vector<datatype> c        //empty vector
vector<datatype> c(n)     //vector size is n,default value==0
vector<datatype> c(n,val) //vector size is n,all element value==val
posted @ 2022-03-27 00:30  越菜越自信  阅读(23)  评论(0)    收藏  举报