vector <int> V[] 和 vector< vector<int> > V 的区别
Difference between vector <int> V[] and vector< vector<int> > V
vector <int> V[]
和 vector< vector<int> > V
都是 二维数组 .
但是它们之间的区别是什么,我们在什么地方使用它们?请简单说明一下。
vector<int> V[]
是一个 向量数组 。
vector< vector<int> > V
是一个向量的向量。
使用数组是 C 风格编码,使用向量是 C++ 风格编码.
引用 cplusplus.com ,
Vectors are sequence containers representing arrays that can change in
size.
Just like arrays, vectors use contiguous storage locations for their
elements, which means that their elements can also be accessed using
offsets on regular pointers to its elements, and just as efficiently
as in arrays. But unlike arrays, their size can change dynamically,
with their storage being handled automatically by the container.
TL;DR:
如果您想使用 固定数量 个 std::vector
元素,您可以使用 vector <int> V[]
.
当您想使用 std::vector
的 动态数组 时,您可以使用 vector< vector<int> > V
.
一个区别是,尽管两者都可以用相同的方式初始化,例如
vector<int> V1[] {{1,2,3}, {4,5,6}};
vector<vector<int>> V2 {{1,2,3}, {4,5,6}};
并访问
cout << V1[0].back() << endl;
cout << V2[0].back() << endl;
V1 无法增长。你不能使 V1.push_back(...) 因为它不是矢量对象。它只是一个数组。第二个是动态的。你可以随心所欲地种植它。
vector V[]只是一个固定数组;所以你只能 add/modify 到上限。它本身不是向量,因此有固定的大小限制。
然而 vector< vector > V 是一个动态向量,它的大小可以动态增加。
vector<vector<int>> v(26);
是一个包含向量的向量。在此示例中,您有一个包含 26 个向量的向量。代码 v[1].push_back(x)
表示 x
被推回到向量中的第一个向量。
vector<int> a[26];
是向量数组。换句话说,一个包含 26 个整数向量的一维数组。代码 a[1].push_back(x);
将 x
推回数组的第一个索引。
vector<int> v[]
是向量数组。也就是说,它是一个包含向量作为其元素的数组。
所以,你不能改变数组部分的大小,但我们可以添加它的元素,它是向量。
例如,
1.vector<int> v1[] = {{1},{2},{3}}; // array that contains 3 vector elements.
2.vector<vector<int>> v2 = {{1},{2},{3}}; // vector that contains 3 vector elements.
因此,首先我们无法更改 v 的大小,但我们可以向其元素添加或删除元素,因为它是一个向量。
v1.push_back(4); // this will give an error since we cannot the predefined size of array.
v1[1].push_back(4); // this is acceptable since we are changing the vector part.
这使得 v1 {{1},{2,4},{3}}
对于第二个,我们可以更改整体大小及其元素。
v2.push_back(4); // this is acceptable since it is vector.
vector <int> V[]
和 vector< vector<int> > V
都是 二维数组 .
但是它们之间的区别是什么,我们在什么地方使用它们?请简单说明一下。
vector<int> V[]
是一个 向量数组 。
vector< vector<int> > V
是一个向量的向量。
使用数组是 C 风格编码,使用向量是 C++ 风格编码.
引用 cplusplus.com ,
Vectors are sequence containers representing arrays that can change in size.
Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.
TL;DR:
如果您想使用 固定数量 个 std::vector
元素,您可以使用 vector <int> V[]
.
当您想使用 std::vector
的 动态数组 时,您可以使用 vector< vector<int> > V
.
一个区别是,尽管两者都可以用相同的方式初始化,例如
vector<int> V1[] {{1,2,3}, {4,5,6}};
vector<vector<int>> V2 {{1,2,3}, {4,5,6}};
并访问
cout << V1[0].back() << endl;
cout << V2[0].back() << endl;
V1 无法增长。你不能使 V1.push_back(...) 因为它不是矢量对象。它只是一个数组。第二个是动态的。你可以随心所欲地种植它。
vector V[]只是一个固定数组;所以你只能 add/modify 到上限。它本身不是向量,因此有固定的大小限制。 然而 vector< vector > V 是一个动态向量,它的大小可以动态增加。
vector<vector<int>> v(26);
是一个包含向量的向量。在此示例中,您有一个包含 26 个向量的向量。代码 v[1].push_back(x)
表示 x
被推回到向量中的第一个向量。
vector<int> a[26];
是向量数组。换句话说,一个包含 26 个整数向量的一维数组。代码 a[1].push_back(x);
将 x
推回数组的第一个索引。
vector<int> v[]
是向量数组。也就是说,它是一个包含向量作为其元素的数组。
所以,你不能改变数组部分的大小,但我们可以添加它的元素,它是向量。
例如,
1.vector<int> v1[] = {{1},{2},{3}}; // array that contains 3 vector elements.
2.vector<vector<int>> v2 = {{1},{2},{3}}; // vector that contains 3 vector elements.
因此,首先我们无法更改 v 的大小,但我们可以向其元素添加或删除元素,因为它是一个向量。
v1.push_back(4); // this will give an error since we cannot the predefined size of array.
v1[1].push_back(4); // this is acceptable since we are changing the vector part.
这使得 v1 {{1},{2,4},{3}}
对于第二个,我们可以更改整体大小及其元素。
v2.push_back(4); // this is acceptable since it is vector.