增加向量中结构的大小会发生什么?(What happens when you increase the size of a struct in a vector?)

编程入门 行业动态 更新时间:2024-10-28 09:24:15
增加向量中结构的大小会发生什么?(What happens when you increase the size of a struct in a vector?)

我正在研究的一个问题涉及一个大的树结构。 最初我是通过new新节点并将它们附加到父节点等来创建树。 这花了很长时间。 一位朋友建议我放弃动态内存分配,并将树结构化为具有子词偏移量的数组。 我之前没有这样做过,所以我对实际发生的事情有一些疑问。

这是超基本的,没有任何安全检查的例子:

struct DataStructure { std::vector<Entry> mCollection; }; struct Entry { char mValue; std::vector<unsigned int> mOffsetCollection; //a vector of indexes that are offsets to other entries. };

我很好奇当我向这个结构添加更多数据时会发生什么。 如果我加了

DataStructure d; Entry entry; entry.mValue = 'a'; d.push_back(entry); . . .//add some more entries... . . //now suppose I add a bunch of offsets to these various entries in my array. Entry& firstEntry = d.at(0); firstEntry.mOffsetCollection.push_back(4); firstEntry.mOffsetCollection.push_back(9); firstEntry.mOffsetCollection.push_back(32); ..

所以第一个条目的大小正在增加。 究竟发生了什么? 我刚刚通过一个小例子,似乎工作正常。 数据结构中的其他条目不受影响。 我最初担心的是,如果结构的大小变大,它可能会遇到阵列中的下一个项目,但我想这不会发生。 这让我意识到我真的不知道幕后发生了什么。 DataStructure中的vector<Entry>是否必须重新分配内存?

A problem I'm working on involves a large tree structure. Initially I was creating the tree by newing new nodes and attaching them to their parent node and such. This was taking an awful long time. A friend suggested instead I forgo the dynamic memory allocation and structure the tree as an array with offsets to child words. I've not done this before and so I have some questions about what actually happens.

Here's the ultra basic, no safety checks on anything example:

struct DataStructure { std::vector<Entry> mCollection; }; struct Entry { char mValue; std::vector<unsigned int> mOffsetCollection; //a vector of indexes that are offsets to other entries. };

I'm curious about what happens as I add more data to this structure. If I added

DataStructure d; Entry entry; entry.mValue = 'a'; d.push_back(entry); . . .//add some more entries... . . //now suppose I add a bunch of offsets to these various entries in my array. Entry& firstEntry = d.at(0); firstEntry.mOffsetCollection.push_back(4); firstEntry.mOffsetCollection.push_back(9); firstEntry.mOffsetCollection.push_back(32); ..

So the size of this first Entry is increasing. What exactly happens? I just ran through through a small example and it seemed to work fine. The other entries in the data structure were unaffected. I was initially concerned that perhaps if the size of the structure became large it would run into the next item in the array but I suppose that isn't happening. It makes me realize that I don't really know what is going on behind the scenes. Is the vector<Entry> in DataStructure d having to reallocate memory?

最满意答案

你没有使用动态分配,但是矢量类是。 内部vector<int>将其可变长度数组存储在您放入外部向量的Entry对象的外部。

You're not using dynamic allocation, but the vector class is. The internal vector<int> is storing its variable-length array externally to the Entry object that you're putting in the outer vector.

更多推荐

本文发布于:2023-08-04 14:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417604.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   大小   发生   结构   vector

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!