使用包含结构的 Vector 的指针问题

发布于 2024-12-05 11:33:58 字数 769 浏览 1 评论 0 原文

我目前真的很困惑如何解决以下问题:

struct node {
    int a;
    node* b;
}

我将有两个没有父节点的根节点,然后有更多节点指向其父节点

vector<node> IholdAllTheNodes;

node noparent1; <--- these get returned from a method
node noparent2;

IholdAllTheNodes.pushback(noparent1);
IholdAllTheNodes.pushback(noparent2);

node withparent1; <---- i am happily points to noparent1
node withparent2; <-----i am happily points to noparent2

到目前为止没有任何问题,

IholdAllTheNodes.pushback(withparent1) <<<< oops this causes all the pointers to move.

现在都工作得很好,withparent 节点不再指向其父节点,因为我已经移动它们了。

我的问题只是如何在不移动原始节点的指针的情况下向节点向量添加更多节点。 (我无法获得向量的固定大小)

如果有人有时间解释,为什么即使推回添加到向量列表的末尾,先前信息的位置也会发生变化?

I am currently really stuck on how to solve the following problem:

struct node {
    int a;
    node* b;
}

I will have two root nodes without parents and then to more nodes pointing to their parents

vector<node> IholdAllTheNodes;

node noparent1; <--- these get returned from a method
node noparent2;

IholdAllTheNodes.pushback(noparent1);
IholdAllTheNodes.pushback(noparent2);

node withparent1; <---- i am happily points to noparent1
node withparent2; <-----i am happily points to noparent2

No problems up to here all working awesomely

IholdAllTheNodes.pushback(withparent1) <<<< oops this causes all the pointers to move.

now the withparent nodes are no longer pointing to their parents because I have moved them.

My question is simply how do I add more nodes to my vector of nodes without moving the pointers of the original nodes. (I cant get a fixed size for the vector)

if anyone has the time to explain, why even though pushback is adding to the end of the vector list is the location of the previous information changing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦境 2024-12-12 11:33:58

致电 reserve

IholdAllTheNodes.reserve(MAX_SIZE);

您可以提前 以避免重新分配。它要求您预先知道 MAX_SIZE。

或者,

  • 建议:将其设为 指针容器
  • 使其成为std::vector; > (类似但效率较低)
  • 使其成为 std::vector (类似但乏味且容易出错)
  • 建议:避免如果可以的话使用指针。按 size_t整数)索引将节点寻址到 IholdAllTheNodes

You could call reserve

IholdAllTheNodes.reserve(MAX_SIZE);

up front to avoid reallocation. It requires you to know MAX_SIZE up front.

Alternatively,

  • suggested: make it a Pointer Container
  • make it a std::vector<shared_ptr<node> > (similar but less efficient)
  • make it a std::vector<node*> (similar but tedious and error-prone)
  • suggested: avoid using pointers if you can. Address the nodes by size_t (integer) index into IholdAllTheNodes instead
盛夏尉蓝 2024-12-12 11:33:58

您的问题是您按向量中的值存储节点。它们被复制,可能被复制很多次。每次一个节点都会被放置在不同的内存地址中。结果,您将收到指向它的不同指针,并且旧指针将指向无效位置。

正如其他答案所建议的,使用任何类型的指针将节点存储在向量中。我会推荐 boost::ptr_vectorstd::vector; >

编辑:

只是不同意保留建议:

这很危险。您需要在所有可能的地方放置大的粗体警告,以便将来修改此代码的人不能忽略它。因为当您向矢量添加超过 MAX_SIZE 个元素时,您的太空飞船将在太阳上结束燃烧。在这种情况下,我建议使用 boost::array 因为它的大小是固定的(因此使您的假设更加明显)并且至少在调试版本中捕获缓冲区溢出

Your problem is that you store nodes by value in vector. They are copied, potentially many times. Each time a node will be placed in different memory address. As result you'll receive different pointers to it, and old pointers will point to invalid location.

As other answers suggested, use any kind of pointers to store your nodes in vector. I would recommend boost::ptr_vector<node> or std::vector<boost::shared_ptr<node> >

EDIT:

Just to disagree with reserve recommendations:

It's dangerous. You need to put large bold warnings in all possible places so people who'll modify this code in the future cannot omit it. Because when you'll add more than MAX_SIZE elements to your vector, your space ship will end burning on Sun. In this case I would recommend to use boost::array since it's fixed size (so making your assumption more obivious) and catches buffer overflow at least in Debug builds

迟到的我 2024-12-12 11:33:58

调用具有特定大小的 IholdAllTheNodes.reserve() ,它将保证进一步的插入不会使指针无效,直到达到这样的保留量。

Call IholdAllTheNodes.reserve() with a specific size, and it will guarantee that further insertions won't invalidate the pointers until such reserved ammount is reached.

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