[英]Are newly allocated elements of std::vector<int> initialized to 0?
Let us say, we use std::vector<int>
or std::vector<long>
. 让我们说,我们使用
std::vector<int>
或std::vector<long>
。 As the vector grows in size, would the newly allocated elements be initialized to 0 by default, or the programmer needs to 0 them explicitly? 随着向量的大小增加,默认情况下新分配的元素是否会被初始化为0,或者程序员是否需要显式地将它们归零?
New elements are value-initialised: 新元素是价值初始化的:
[C++11: 23.3.6.3/9]:
void resize(size_type sz);
[C++11: 23.3.6.3/9]:
void resize(size_type sz);
Effects: Ifsz <= size()
, equivalent toerase(begin() + sz, end());
效果:如果
sz <= size()
,相当于erase(begin() + sz, end());
.。 If
size() < sz
, appendssz - size()
value-initialized elements to the sequence.如果
size() < sz
,则将sz - size()
值初始化元素追加到序列中。
For both int
and long
this means 0
: 对于
int
和long
这意味着0
:
[C++11: 8.5/7]:
To value-initialize an object of typeT
means :[C++11: 8.5/7]:
对类型为T
的对象进行值初始化意味着 :
- if
T
is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor forT
is called (and the initialization is ill-formed ifT
has no accessible default constructor);如果
T
是具有用户提供的构造函数(12.1)的(可能是cv限定的)类类型(第9节),则调用T
的默认构造函数(如果T
没有可访问的默认构造函数,则初始化是错误的) ;- if
T
is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, ifT
's implicitly-declared default constructor is non-trivial, that constructor is called.如果
T
是一个(可能是cv限定的)非联合类类型而没有用户提供的构造函数,那么该对象是零初始化的,如果T
的隐式声明的默认构造函数是非平凡的,则调用该构造函数。- if
T
is an array type, then each element is value-initialized;如果
T
是数组类型,那么每个元素都是值初始化的;- otherwise, the object is zero-initialized.
否则,该对象被零初始化。
An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to “constructed” objects, objects “for which the constructor has completed,” etc., even if no constructor is invoked for the object's initialization.
值初始化的对象被视为构造,因此受本国际标准的规定适用于“构造”对象,“构造函数已完成的对象”等,即使没有为该对象调用构造函数也是如此。初始化。
Note, though, that this is not talking about the "reserved" space at the end of the vector. 但请注意,这并不是在向量的末尾讨论“保留”空间。 This space does not contain any valid elements, zero-initialised or otherwise.
此空间不包含任何有效元素,零初始化或其他。 This answer and the standard wording talks only about the elements you get when you perform a
resize
without specifying an explicit value for your new elements. 此答案和标准措辞仅讨论在执行
resize
未获得新元素的显式值时获得的元素。
Per Paragraph 23.3.6.3/9 of the C++11 Standard (about std::vector::resize()
): 根据C ++ 11标准的第23.3.6.3/9段(关于
std::vector::resize()
):
void resize(size_type sz);
Effects: If sz <= size(), equivalent to erase(begin() + sz, end());.
效果:如果sz <= size(),相当于erase(begin()+ sz,end());. If size() < sz, appends sz - size() value-initialized elements to the sequence.
如果size()<sz,则将sz-size() 值初始化元素追加到序列中。
Moreover, per Paragraph 8.5/7 of the Standard: 此外,根据标准第8.5 / 7段:
To value-initialize an object of type T means:
对值类型T的对象进行值初始化意味着:
— if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
- 如果T是具有用户提供的构造函数(12.1)的(可能是cv限定的)类类型(第9节),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是错误的);
— if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T's implicitly-declared default constructor is non-trivial, that constructor is called.
- 如果T是一个(可能是cv限定的)非联合类类型而没有用户提供的构造函数,那么该对象是零初始化的,如果T的隐式声明的默认构造函数是非平凡的,则调用该构造函数。
— if T is an array type, then each element is value-initialized;
- 如果T是数组类型,则每个元素都是值初始化的;
— otherwise, the object is zero-initialized .
- 否则,对象被零初始化 。
This means that in the case of int
the newly created elements are value-initialized to 0
. 这意味着在
int
的情况下,新创建的元素被初始化为0
。
Yes, when the std::vector
changes size (perhaps by doing std::vector::resize
), any new elements will be value-initialized . 是的,当
std::vector
改变大小时(可能通过执行std::vector::resize
),任何新元素都将被初始化 。 For a type such as int
or long
, value-initialization causes zero-initialization which, as the name suggests, will set the value to 0. 对于诸如
int
或long
类的类型,值初始化会导致零初始化,顾名思义,它会将值设置为0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.