简体   繁体   English

矢量模板的make_heap方法如何工作?

[英]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) 当我使用stl向量容器的“ make_heap”方法时,它会更改元素的物理地址还是只是在逻辑上更改顺序(通过某些类成员)

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? 这是向量的make_heap的处理方式吗?

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. 我猜您是用vector s迭代器作为参数引用std::make_heap 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 . 在这种情况下,它只会重新排列向量中的元素,并且不会引起任何重新分配,因此您可以放心地假设,即使在调用make_heap之后,向量元素的指针(或迭代器)仍将保持有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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