简体   繁体   中英

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM