C++中的push_heap函数有什么作用?做?

发布于 2024-12-11 18:40:39 字数 831 浏览 0 评论 0原文

我想知道带有三个参数的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2024-12-18 18:40:39

您错误地使用了push_heap。

初始化向量后,您需要将其按堆顺序放置:

std::make_heap(vector1.begin(), vector1.end());

要将更多元素添加到堆中,您需要首先将每个元素推到向量的后面,然后调用push_heap:

vector1.push_back(42);
std::push_heap(vector1.begin(), vector1.end());

最后,删除堆中的第一个元素,您需要调用 pop_heap,然后从向量中弹出最后一个元素:

std::pop_heap(vector1.begin(), vector1.end());
vector1.pop_back();

三参数堆函数允许您指定一个比较方法来控制堆顺序,您的做法是正确的。

手动调用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:

std::make_heap(vector1.begin(), vector1.end());

To add further elements into the heap, you need to first push each to the back of the vector, then call push_heap:

vector1.push_back(42);
std::push_heap(vector1.begin(), vector1.end());

Finally, to remove the first element in the heap, you need to call pop_heap, followed by popping the last element from the vector:

std::pop_heap(vector1.begin(), vector1.end());
vector1.pop_back();

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.

勿忘初心 2024-12-18 18:40:39

此函数不会将一系列值转换为堆!

std::push_heap(first, last [, comp])

假设范围 [first,last-1) 已经是有效的堆 并将位置 last-1 处的值推入堆中,将其移动到正确的位置以保持堆要求有效。它使用 < 运算符来确定元素的顺序或用户指定的比较器。

This function does not turn a range of values into a heap!

std::push_heap(first, last [, comp])

assumes that the range [first,last-1) is already a valid heap and pushes the value at position last-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文