boost :: fibonacci_heap:带有比较器重新定义圆形定义错误的嵌套定义

boost::fibonacci_heap: Nested define of handle with comparator redefined circular definition errors

本文关键字:定义 错误 嵌套 新定义 heap fibonacci 比较器 boost      更新时间:2023-10-16

提升文档和以前的堆栈溢出都提供了如何定义自定义比较函数的工作示例,并在boost堆的节点类型中包含句柄。但是,当我将这两个功能(自定义定义比较函数和节点类型中的句柄(结合起来时,我会发现错误报告不完整类型的" struct compare_node"。

https://www.boost.org/doc/libs/1_63_0/doc/html/heap/heap/concepts.html#heap.concepts.mutability

使用boost fibonacci_heap

定义比较fibonacci堆的功能

减少斐波那契堆中的操作,boost

除了预先定义节点的两个结构和compare_node之外,我不确定要解决圆形性,同时仍将手柄固定为节点结构中的成员。

#include <boost/heap/fibonacci_heap.hpp>
struct compare_Node; //predefine to avoid circular issues
struct Node; //predefine to avoid circular issues
using fib_heap = boost::heap::fibonacci_heap<struct Node*,
    boost::heap::compare<struct compare_Node>>;
// 6-byte struct total
struct Node {
    double value; 
    fib_heap::handle_type* handle; // orig
};
// override for min_heap
struct compare_Node
{
    bool operator() (struct Node* const n1, struct Node* const n2) const
    {
        return n1->value > n2->value;
    }
};
int main() {
    fib_heap heap;
    return 0;
}

仅在operator()的声明中定义compare_Node。指向Node的指针不需要Node定义。Node定义后,您可以添加operator()的主体:

struct compare_Node
{
    bool operator() (struct Node* const n1, struct Node* const n2) const;
};
using fib_heap = boost::heap::fibonacci_heap<struct Node*,
    boost::heap::compare<struct compare_Node>>;
// 6-byte struct total
struct Node {
    double value; 
    fib_heap::handle_type* handle; // orig
};
bool compare_Node::operator() (struct Node* const n1, struct Node* const n2) const
{
        return n1->value > n2->value;
}

在线演示。