How does make_heap method of vector template work?
When I use "make_heap" method of stl vector container, does it change the physical addresses of the elements or it just changes the order logically (through some class member)
Let me explain more:
Suppose I implement Heap using the following structure开发者_开发百科
struct heap
{
int cost;
struct heap* leftChild;
struct heap* rightChild;
};
I can make sure that only the pointers inside the structure change. but not the physical addresses. Is this the way vector's make_heap does it?
The reason I am asking this question is that I have another object that points to the elements of heap. I want to make sure that I need not update this pointer even if the heap changes.
I guess you are referring std::make_heap
with vector
s iterators as parameters. In such case, it only rearranges the elements within the vector and will not cause any reallocations, so you can safely assume that the pointers (or iterators) to the vector elements will remain valid even after calling make_heap
.
精彩评论