C++ - 未初始化的 vector<int> 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5222404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ - value of uninitialized vector<int>
提问by anon
I understand from the answer to this questionthat values of global/static uninitialized int will be 0. The answer to this onesays that for vectors, the default constructor for the object type will be called.
我从答案明白这个问题是全局/静态初始化为int值将为0回答这一说,对向量,对象类型的默认构造函数将被调用。
I am unable to figure out - what happens when I have vector<int> v(10)
in a local function. What is the default constructor for int? What if I have vector<int> v(10)
declared globally?
我无法弄清楚 - 当我vector<int> v(10)
在本地函数中时会发生什么 。int的默认构造函数是什么?如果我已经vector<int> v(10)
在全球范围内声明了怎么办?
What I am seeing is that vector<int> v(10)
in a local function is resulting in variables being 0 - but I am not sure if that is just because of my compiler or is the fixed expected behaviour.
我看到的是, vector<int> v(10)
在本地函数中导致变量为 0 - 但我不确定这是否只是因为我的编译器或者是固定的预期行为。
回答by Mark B
The zero initialization is specified in the standard as default zero initialization/value initialization for builtin types, primarily to support just this type of case in template use.
零初始化在标准中被指定为内置类型的默认零初始化/值初始化,主要是为了在模板使用中支持这种类型的情况。
Note that this behavior is different from a local variable such as int x;
which leaves the value uninitialized (as in the C language that behavior is inherited from).
请注意,此行为与局部变量不同,例如int x;
它使值未初始化(如在继承行为的 C 语言中)。
回答by CashCow
It is not undefined behaviour, a vector automatically initialises all its elements. You can select a different default if you want.
这不是未定义的行为,向量会自动初始化其所有元素。如果需要,您可以选择不同的默认值。
The constructor is:
构造函数是:
vector( size_type, T t = T() )
vector( size_type, T t = T() )
and for int, the default type (returned by int()
) is 0.
对于 int,默认类型(由 返回int()
)为 0。
In a local function this:
在本地函数中:
int x;
int x;
is not guaranteed to initialise the variable to 0.
不保证将变量初始化为 0。
int x = int();
int x = int();
would do so.
会这样做。
int x();
int x();
sadly does neither but declares a function.
可悲的是,两者都没有,但声明了一个函数。
回答by James McNellis
The constructor you are using actually takes two arguments, the second of which is optional. Its declaration looks like this:
您使用的构造函数实际上有两个参数,其中第二个是可选的。它的声明如下所示:
explicit vector(size_type n, const T& value = T())
The first argument is the number of elements to create in the vector
initially; the second argument is the value to copy into each of those elements.
第一个参数是vector
最初创建的元素数量;第二个参数是要复制到每个元素中的值。
For any object type T
, T()
is called "value initialization." For numeric types, it gives you 0
. For a class type with a default constructor, it gives you an object that has been default constructed using that constructor.
对于任何对象类型T
,T()
都称为“值初始化”。对于数字类型,它为您提供0
. 对于具有默认构造函数的类类型,它会为您提供一个使用该构造函数默认构造的对象。
For more details on the "magic parentheses," I'd recommend reading Michael Burr's excellent answer to the question "Do the parentheses after the type name make a difference with new?"It discusses value initialization when used with new
specifically, but for the most part is applicable to value initialization wherever else it can be used.
有关“魔术括号”的更多详细信息,我建议您阅读 Michael Burr 对“类型名称后的括号是否与 new 有所不同?”这一问题的出色回答。它讨论了new
具体使用时的值初始化,但在大多数情况下,它适用于任何其他可以使用的值初始化。
回答by AProgrammer
By default, vector elements are zero-initialized and not default-initialized. Those are two different but related concepts:
默认情况下,向量元素是零初始化的,而不是默认初始化的。这是两个不同但相关的概念:
zero-initialization is what is done for static objects not having an explicit initialization and what is done for a member given in the initialized list with an initializer of
()
. For basic types, the value used is 0 converted to the type.default-initialization is what is done for not explicitly initialized non static variables and members. For basic types it stay uninitialized.
零初始化是为没有显式初始化的静态对象所做的,以及为初始化列表中给定的成员所做的,初始化器为
()
. 对于基本类型,使用的值是 0 转换为类型。默认初始化是为未显式初始化的非静态变量和成员所做的。对于基本类型,它保持未初始化状态。
(And C++0X introduces value-initialization which is still different).
(而 C++0X 引入了仍然不同的值初始化)。
回答by Platinum Azure
The default initialization for an int
type is to initialize it to 0.
int
类型的默认初始化是将其初始化为 0。
This is true of most (if not all) primitive types: char
will initialize to (char)0
(or '\0'
if you prefer), float
will initialize to 0.0f
, and any pointer initializes to NULL
. For other types, the parameterless constructor is invoked.
大多数(如果不是全部)原始类型都是如此:char
将初始化为(char)0
(或者'\0'
如果您愿意),float
将初始化为0.0f
,并且任何指针初始化为NULL
。对于其他类型,调用无参数构造函数。
In general, the default initialization should happen pretty much whenever you aren't able to specify a constructor (or choose not to).
通常,当您无法指定构造函数(或选择不指定)时,默认初始化应该会发生。