调整大小时,vector.resize() 方法是否调用默认元素构造函数?
我正在尝试以下代码:
struct _Struct2
{
void *ptr;
double dval;
};
struct _Struct
{
float fval;
int ival;
std::vector<_Struct2> data;
};
std::vector<_Struct> vec;
int main()
{
vec.resize( 9 );
for ( int i = 0; i < vec.size(); i++ )
{
_Struct &elem = vec[i];
int len = elem.data.size(); // elem.data is [0]()
}
}
resize(9)
应该分配 9 个 _Struct
类型的元素并且它可以工作。但是 _Struct
类型的每个元素都没有初始化,尤其是 data
元素,它是另一个 std::vector
。我希望将其初始化为空的 std::vector 。我必须手动执行此操作吗?我认为 resize()
方法会调用每个 _Struct
元素的默认构造函数。谢谢
。这里使用的结构的名称只是我首先想到的。这只是一个例子。我的 Visual Studio 告诉我,调试视图中的 elem.data
对应于 [0]()
。
诗。忘记[0]()
。
I am trying the following code:
struct _Struct2
{
void *ptr;
double dval;
};
struct _Struct
{
float fval;
int ival;
std::vector<_Struct2> data;
};
std::vector<_Struct> vec;
int main()
{
vec.resize( 9 );
for ( int i = 0; i < vec.size(); i++ )
{
_Struct &elem = vec[i];
int len = elem.data.size(); // elem.data is [0]()
}
}
The resize(9)
should allocate 9 elements of type _Struct
and it works. But every element of type _Struct
is not initialized, especially the data
element which is another std::vector
. I would like it to be initialized to the empty std::vector
. Do I have to do that manually? I thought that the resize()
method would have called the default constructor of every _Struct
element. Thx
Ps. The names of the structs used here are just the first things that come to my mind. This is just an example. My Visual Studio tells me that elem.data
corresponds in the debug view to [0]()
.
Ps. Forget the [0]()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,它不会调用默认元素构造函数。
std::vector
从不在内部调用默认构造函数(在 C++11 中会调用,但在规范的早期版本中不会)。vector::resize
的完整签名如下所示:它有第二个参数(具有默认参数值)。然后,第二个参数用作“源”对象,以通过复制构造函数初始化新元素。
换句话说,您对
resize
的调用实际上相当于,当您向
vector 提供“源”对象时,您调用了默认构造函数: :resize
,即使您没有注意到这一点。啊? “未初始化”?我不知道这意味着什么,考虑到在您的示例代码中,由
resize
创建的每个新元素都已如上所述完美初始化:它是从_Struct( )
元素,您隐式提供给resize
作为第二个参数。每个_Struct::fval
和_Struct::ival
为零,每个_Struct::data
都是一个空向量。(在原始 C++98 中
_Struct::fval
和_Struct::ival
将保持未初始化状态,因为 TC1 之前的 C++98 不支持 值初始化,但即使在原始 C++98 中,_Struct::data
也会被初始化为空向量。每个
_Struct::data
向量都已初始化为空向量。是什么让你相信事实并非如此?PS 以
_
开头,后跟大写字母的名称由实现保留。您不被允许使用它们。No it doesn't call default element constructor.
std::vector
never calls default constructors internally (it does in C++11, but not in earlier versions of the specification).The full signature for
vector::resize
looks as followsI.e. it has a second parameter (with default argument value). That second parameter is then used as a "source" object to initialize new elements by copy-constructor.
In other words, your call to
resize
is actually equivalent tomeaning that it is you who called the default constructor when you supplied that "source" object to
vector::resize
, even though you didn't notice that.Huh? "Not initialized"? I don't know what that is supposed to mean, considering that in your sample code every new element created by
resize
is perfectly initialized as described above: it is copy-initialized from a_Struct()
element you implicitly supplied toresize
as the second argument. Each_Struct::fval
and_Struct::ival
is zero, and each_Struct::data
is an empty vector.(In the original C++98
_Struct::fval
and_Struct::ival
will remain uninitialized, since pre-TC1 C++98 didn't support value-initialization. But_Struct::data
will be initialized to an empty vector even in the original C++98).Every
_Struct::data
vector is already initialized as an empty vector. What made you believe that it isn't?P.S. Names that begin with
_
followed by an uppercase letter are reserved by the implementation. You are not allowed to use them.