vector< int>之间的区别是什么? v(N)和向量<整数> v [N]? [英] what is difference between vector<int> v(N) and vector < int > v [N]?
问题描述
什么是vector< int > v(N)
?
-
这是一个最大大小为
N
的数组(例如vector<int> v
)吗?
与vector<int> v[N]
一样吗?
我认为这是第一个(如果我错了,请纠正我).
I think it is the first one(correct me if I am wrong).
什么是vector< vector< int > > V(N)
?它像最大尺寸为N
的2D阵列吗?
vector < int > v[N]
和vector< vector< int > >v(N)
有什么区别?
What is vector< vector< int > > V(N)
? Is it like 2D array with maximum size N
?
What is the difference between vector < int > v[N]
and vector< vector< int > >v(N)
?
推荐答案
假设
-
N
是某种整数, - 某处有一个
#include <vector>
, - 某处有
using namespace std;
或using std::vector;
...
N
is some kind of integer,- there is an
#include <vector>
somewhere, - there is either a
using namespace std;
or ausing std::vector;
somewhere...
这是类型为std::vector< int >
的对象v
的声明,已初始化为保存N
对象(类型为int
的对象),这些对象已默认初始化(即不确定,因为int
为没有定义默认初始化的POD类型.
This is the declaration of an object v
, of type std::vector< int >
, initialized to hold N
objects (of type int
), which are default-initialized (i.e., indeterminate, since int
is a POD type for which no default initialization is defined).
vector
构造函数的文档 –这种情况(2).
Documentation of vector
constructors -- this one is case (2).
螺旋规则-需要适应C ++,但仍然适用良好的开端.
Spiral rule -- which needs some adapting to C++ but is still a good start.
它不是不是动态数组行vector v
",它是 向量.
It is not "a dynamic array line vector v
", it is a vector.
不,它与vector v[N]
(甚至无法编译)相同. 也 不与vector<int> v[N]
相同-它是由N
个不同的vector<int>
对象组成的数组.
And no, it is not the same as vector v[N]
(which would not even compile). It is also not the same as vector<int> v[N]
-- that would be an array of N
different vector<int>
objects.
现在,如果它是第一个,那么
vector< vector< int > > V(N)
是什么?
由于它不是第一个,所以我还必须回答吗? :-D
Since it's not the first one, do I still have to answer this? :-D
vector< vector< int > > V(N);
这是类型为vector< vector< int > >
的对象V
的声明,即"int-vectors的向量",已初始化为保存N
默认初始化的对象...
That is the declaration of an object V
of type vector< vector< int > >
, i.e. "vector of int-vectors", initialized to hold N
default-initialized objects...
...即,一个包含vector< int >
个N
个对象的向量-依次为空(因为空"是默认初始化的向量).
...i.e., a vector holding N
objects of vector< int >
-- which are, in turn, empty (because "empty" is what default-initialized vectors are).
C ++具有...
C++ has...
- 数组(
int v[N];
),其工作原理与C数组完全相同. - std :: vector (
std::vector< int > v;
),其大小是动态的. - std :: array (
std::array< int, N >;
),它的大小像C数组一样是静态的,但确实提供了std::vector
的一些优点.
- The array (
int v[N];
), which works exactly like the C array. - The std::vector (
std::vector< int > v;
), which is dynamic in size. - The std::array (
std::array< int, N >;
), which is static in size like the C array, but does offer some of the niceties ofstd::vector
.
您需要准确地指代您所指的内容.这些是非常独特的类型,具有独特的功能集.
You need to be exact about what you are referring to. These are quite distinct types with a distinct feature set.
更新:
最近几次修改后,很明显您的真实问题是:
With your latest couple of edits, it became clear that your real question is:
什么是
vector
?
这是一个C ++类模板,实现了一个容器,该容器可以容纳给定类型( like C数组)的连续元素,但是具有动态大小( C数组).
It's a C++ class template implementing a container that can hold contiguous elements of a given type (like a C array), but is dynamically-sized (unlike a C array).
通常来说,除了某些 really 特殊情况外,您不再在C ++代码中使用C数组.您将std::string
用于字符串,并将std::vector
用于(几乎)在C中使用数组的其他所有功能.
Generally speaking, you don't use C arrays in C++ code anymore, except for some really special cases. You use std::string
for strings, and std::vector
for (almost) everything else you used arrays for in C.
这篇关于vector< int>之间的区别是什么? v(N)和向量<整数> v [N]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!