调整大小时,vector.resize() 方法是否调用默认元素构造函数?

发布于 2024-12-04 15:11:05 字数 833 浏览 0 评论 0原文

我正在尝试以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回心转意 2024-12-11 15:11:05

不,它不会调用默认元素构造函数。 std::vector 从不在内部调用默认构造函数(在 C++11 中会调用,但在规范的早期版本中不会)。

vector::resize 的完整签名如下所示

void resize(size_type sz, T c = T());

:它有第二个参数(具有默认参数值)。然后,第二个参数用作“源”对象,以通过复制构造函数初始化新元素。

换句话说,您对 resize 的调用实际上相当于

vec.resize( 9, _Struct() );

,当您向 vector 提供“源”对象时,调用了默认构造函数: :resize,即使您没有注意到这一点。

但是 _Struct 类型的每个元素都没有初始化,尤其是
数据元素,这是另一个 std::vector。

啊? “未初始化”?我不知道这意味着什么,考虑到在您的示例代码中,由 resize 创建的每个新元素都已如上所述完美初始化:它是从 _Struct( ) 元素,您隐式提供给 resize 作为第二个参数。每个 _Struct::fval_Struct::ival 为零,每个 _Struct::data 都是一个空向量。

(在原始 C++98 中 _Struct::fval_Struct::ival 将保持未初始化状态,因为 TC1 之前的 C++98 不支持 值初始化,但即使在原始 C++98 中,_Struct::data 也会被初始化为空向量。

我希望它被初始化为空的 std::vector。

每个 _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 follows

void resize(size_type sz, T c = T());

I.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 to

vec.resize( 9, _Struct() );

meaning 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.

But every element of type _Struct is not initialized, especially the
data element, which is another std::vector.

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 to resize 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).

I would like it to be initialized to the empty std::vector.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文