new delete

Dynamically allocated memory, similar to malloc / free

 

Look at how you can use

Plastic

    int *p = new int;
    *p = 5;

Or directly initialized at the same time the new

    int *p = new int(5);
delete p;

 

character

    char *p = new char('a');
    delete p;

 

Array

int *p = new int[5];
delete []p

 

Two-dimensional array:

New online information such two-dimensional array: int ** p = new int [4] [5] ; this is the error of int [4] [5] This type of data do not match and ** p

We can do this

    int **p = new int*[4];
    for (auto i = 0; i < 4; i++)
    {
        p[i] = new int[5];
    }

Or directly with a vector

    vector<vector<int>> p(4);
    p[1].push_back(4);
    p[1].push_back(6);
    p[2].push_back(1);
    cout<<p[1][1]<<endl;

 

Guess you like

Origin www.cnblogs.com/qifeng1024/p/12652002.html