简体   繁体   English

是新分配的std :: vector元素 <int> 初始化为0?

[英]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: 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() 值初始化元素追加到序列中。

For both int and long this means 0 : 对于intlong这意味着0

[C++11: 8.5/7]: To value-initialize an object of type T 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 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. 否则,该对象被零初始化。

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. 对于诸如intlong类的类型,值初始化会导致零初始化,顾名思义,它会将值设置为0。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除std :: vector的元素(并更改大小),而不会影响已分配的内存 - Remove elements (and change size) of std::vector without affecting allocated memory 在堆上分配元素的 std::vector - 我需要 5 的规则吗? - std::vector with elements allocated on the heap - do I need rule of 5? 可能吗? 的std ::矢量 <double> my_vec(SZ); 已分配但未初始化或已填写 - Is it possible? std::vector<double> my_vec(sz); which is allocated but not initialized or filled 在堆栈上分配的向量元素? - vector elements allocated on stack? 动态分配的数组或std :: vector - Dynamically allocated arrays or std::vector 为什么 std::array 不能<std::pair<int,int> , 3&gt; 使用嵌套初始化列表进行初始化,但 std::vector <std::pair<int,int> &gt; 可以吗? </std::pair<int,int></std::pair<int,int> - Why can't std::array<std::pair<int,int>, 3> be initialized using nested initializer lists, but std::vector<std::pair<int,int>> can? 分配给std :: vector的释放内存的正确方法 <int> 在代码块结束之前 - Right way to Free memory allocated to std::vector<int> before end of code block 是否在 std::vector 上调整大小<int>将新元素设置为零?</int> - Does resize on a std::vector<int> set the new elements to zero? 最大编号可以存储在 std::vector 中的元素<int></int> - Max no. of elements that can be stored in the std::vector<int> 使用类型对的元素对 std::vector 进行排序<int, string> , 但顺序相反</int,> - Sorting a std::vector with elements of type pair<int, string>, but in reverse order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM