我目前真的很困惑如何解决以下问题:
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?
发布评论
评论(3)
致电 reserve
您可以提前 以避免重新分配。它要求您预先知道 MAX_SIZE。
或者,
std::vector; >
(类似但效率较低)std::vector
(类似但乏味且容易出错)size_t
(整数)索引将节点寻址到IholdAllTheNodes
中You could call reserve
up front to avoid reallocation. It requires you to know MAX_SIZE up front.
Alternatively,
std::vector<shared_ptr<node> >
(similar but less efficient)std::vector<node*>
(similar but tedious and error-prone)size_t
(integer) index intoIholdAllTheNodes
instead您的问题是您按向量中的值存储节点。它们被复制,可能被复制很多次。每次一个节点都会被放置在不同的内存地址中。结果,您将收到指向它的不同指针,并且旧指针将指向无效位置。
正如其他答案所建议的,使用任何类型的指针将节点存储在向量中。我会推荐
boost::ptr_vector
或std::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>
orstd::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 useboost::array
since it's fixed size (so making your assumption more obivious) and catches buffer overflow at least in Debug builds调用具有特定大小的 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.