简体   繁体   中英

What is the difference between "vector<int> v[]" and "vector<vector<int>> v"

I do not know the difference between vector<int> v[] and vector<vector<int>> v

vector<int>v[] = {{ 0, 1 }, { 2, 3 }};

v[0][0] = 4;
v[0][1] = 2;
v[0].push_back(4);

for (int i = 0; i < v[0].size(); i++)
    cout << v[0][i] << endl;

cout << v[1][0] << endl;

output : 4 2 4 2

vector<vector<int>> v = { {0, 1}, {2, 3} };

v[0][0] = 4;
v[0][1] = 2;
v[0].push_back(4);

for (int i = 0; i < v[0].size(); i++)
    cout << v[0][i] << endl;

cout << v[1][0] << endl;
return 0;

output : 4 2 4 2

Is there a difference in memory or the creation process?

vector<int>v[] = {{ 0, 1 }, { 2, 3 }};

This is an array of vector.


vector<vector<int>> v = { {0, 1}, {2, 3} };

This is vector of vector.


Is there a difference in memory or the creation process?

So of course, yes, there are differences in both memory and creation.

  • The former is a fixed 2D-array of vector. Although the 2nd dimension (vector) can grow or shrink, the 1st dimension is not. It's similar to a [2 x Y] matrix.

  • The latter is vector of vector. So both 1st and 2nd dimensions can change. So it can be similar to [X x Y] matrix, where both X and Y can change at run time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM