[英]Does resize on a std::vector<int> set the new elements to zero?
Consider考虑
#include <vector>
int main()
{
std::vector<int> foo;
foo.resize(10);
// are the elements of foo zero?
}
Are the elements of foo
all zero? foo
的元素都为零吗? I think they are from C++11 onwards.我认为它们是从 C++11 开始的。 But would like to know for sure.但是想确定一下。
Simple answer: Yes简单的回答:是的
Long answer:长答案:
If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n.如果 n 大于当前容器大小,则通过在末尾插入所需数量的元素来扩展内容,以达到 n 的大小。 If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized如果指定了 val,则将新元素初始化为 val 的副本,否则,将它们初始化为值
Now, as value initialization from the standard we get:现在,作为标准的值初始化,我们得到:
— if T is a class type (clause 9) with a user-declared 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) 的 class 类型(第 9 条),则调用 T 的默认构造函数(如果 T 没有可访问的默认构造函数,则初始化是非良构的); — if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized; — 如果 T 是没有用户声明的构造函数的非联合 class 类型,则 T 的每个非静态数据成员和基类组件都是值初始化的; — if T is an array type, then each element is value-initialized; — 如果 T 是一个数组类型,那么每个元素都是值初始化的; — otherwise, the object is zero-initialized — 否则,object 被零初始化
int
falls on otherwise, so it is zero initialized否则int
落在上面,所以它是零初始化的
Are the elements of
foo
all zero?foo
的元素都为零吗?
Yes, this can be seen from std::vector::resize documentation which says:是的,这可以从std::vector::resize 文档中看出:
If the current size is less than count,如果当前大小小于计数,
- additional default-inserted elements are appended附加了其他默认插入的元素
And from defaultInsertable :从defaultInsertable :
By default, this will call placement-new, as by
::new((void*)p) T()
(until C++20)std::construct_at(p)
(since C++20) (that is, value-initialize the object pointed to byp
).默认情况下,这将调用placement-new,如::new((void*)p) T()
(C++20 前)std::construct_at(p)
(C++20 起)(即,p
指向的 object 进行值初始化)。 If value-initialization is undesirable, for example, if the object is of non-class type and zeroing out is not needed, it can be avoided by providing a customAllocator::construct
.如果不需要值初始化,例如,如果 object 是非类类型并且不需要清零,则可以通过提供自定义Allocator::construct
来避免。
(emphasis mine) (强调我的)
Note the T()
in the above quoted statement.请注意上面引用的语句中的T()
。 This means that after the resize foo.resize(10);
这意味着在调整大小之后foo.resize(10);
, elements of foo
will contain value 0
as they were value initialized . , foo
的元素将包含值0
,因为它们是值初始化的。
This member function is overloaded the following way这个成员function通过以下方式重载
void resize(size_type sz);
void resize(size_type sz, const T& c);
For the first function the effect is对于第一个 function 的效果是
Effects: If sz <= size(), equivalent to calling pop_back() size() - sz times.效果:如果 sz <= size(),相当于调用 pop_back() size() - sz 次。 If size() < sz, appends sz - size() default-inserted elements to the sequence如果 size() < sz,则将 sz - size() 默认插入的元素附加到序列中
That is there is used the expression T().那就是使用表达式 T()。 If elements are of the type int
then they are zero-initialized.如果元素是int
类型,那么它们被零初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.