C++中的push_heap函数有什么作用?做?
我想知道带有三个参数的push_heap函数是做什么的?
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
using namespace std;
class HeapCompare_f
{
public:
bool operator() ( int x, int y ) const
{
return x > y;
}
};
int main()
{
vector<int> vector1(5);
for (int i = 0; i < 5; ++i)
vector1[i] = 5-i;
for (int i = 0; i < 5; ++i)
cout << vector1[i];
cout << endl;
push_heap(vector1.begin(), vector1.end(),HeapCompare_f());
for (int i = 0; i < 5; ++i)
cout << vector1[i];
cout << endl;
return 0;
}
这段代码的输出是
54321
15324
另外我想知道如何在 C 中实现该函数?因为我将在我用 C 编写的 A* 算法中使用它
I wonder what does the push_heap function which takes three parameters do?
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
using namespace std;
class HeapCompare_f
{
public:
bool operator() ( int x, int y ) const
{
return x > y;
}
};
int main()
{
vector<int> vector1(5);
for (int i = 0; i < 5; ++i)
vector1[i] = 5-i;
for (int i = 0; i < 5; ++i)
cout << vector1[i];
cout << endl;
push_heap(vector1.begin(), vector1.end(),HeapCompare_f());
for (int i = 0; i < 5; ++i)
cout << vector1[i];
cout << endl;
return 0;
}
The output of this code is
54321
15324
Also I wonder how can I implement that function in C ? Because I will use it in A* algorithm which I am writing in C
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您错误地使用了push_heap。
初始化向量后,您需要将其按堆顺序放置:
要将更多元素添加到堆中,您需要首先将每个元素推到向量的后面,然后调用push_heap:
最后,删除堆中的第一个元素,您需要调用 pop_heap,然后从向量中弹出最后一个元素:
三参数堆函数允许您指定一个比较方法来控制堆顺序,您的做法是正确的。
手动调用push_back和pop_back的原因是堆函数只能看到容器中的迭代器,而无权访问容器本身。由于迭代器不足以修改容器的内容,因此这必须由容器的所有者(您)手动完成。
为了避免您自己处理这些问题,我建议使用
std::priority_queue
。You are using push_heap incorrectly.
After initializing your vector, you need to put it in heap order:
To add further elements into the heap, you need to first push each to the back of the vector, then call push_heap:
Finally, to remove the first element in the heap, you need to call pop_heap, followed by popping the last element from the vector:
The three-parameter heap functions let you specify a compare method to control the heap order, which you are doing correctly.
The reason for the manual push_back and pop_back calls is that the heap functions only see iterators into a container, and do not have access to the container itself. Since iterators are not sufficient to modify the contents of a container, this must be done manually by the owner of the container (you).
To avoid having to deal with any of this yourself, I'd recommend using a
std::priority_queue
.此函数不会将一系列值转换为堆!
假设范围
[first,last-1)
已经是有效的堆 并将位置last-1
处的值推入堆中,将其移动到正确的位置以保持堆要求有效。它使用<
运算符来确定元素的顺序或用户指定的比较器。This function does not turn a range of values into a heap!
assumes that the range
[first,last-1)
is already a valid heap and pushes the value at positionlast-1
into the heap, moving it to the correct position to keep the heap-requirement valid. It uses either the<
operator to determine the ordering of the elements or a user-specified comparator.