如果用vector来存节点,并且一开始没有reserve或者resize,那么用push_back动态开点的时候可能一些元素的地址会改变。
具体原因这里有提到:“If size == capacity before the push_back, a new, larger array is allocated, all of the current data is copied over (including the new element), and the old allocated space is freed.”
所以最好的解决方式是一开始就把vector resize到需要用到的大小(size),或者如果你像我一样就是想要用push_back的话,可以一开始reserve一下需要用到的大小(capacity),这样就不会遇到上面说的size == capacitiy的情况了。