插入优先级队列时出现分段错误

发布于 2024-08-06 01:41:03 字数 674 浏览 4 评论 0原文

我对优先级队列的定义是:

template<typename Node, typename Cmp = std::less<Node> >
struct deref_compare : std::binary_function<Node*,Node*,bool>
{
    deref_compare(Cmp const& cmp = Cmp())
    : cmp(cmp) {}

    bool operator()(Node* a, Node* b) const {
        return (a->getfValue()> b->getfValue());
    }

private:
    Cmp cmp;
};

typedef deref_compare<Node,std::greater<Node> > my_comparator_t;
priority_queue<Node*,vector<Node*>,my_comparator_t> openq; 

我正在做:

openq.push(myNode)

进入 3-4 个节点后,我遇到分段错误。

mynode 不为空。

我该如何解决它?

My definition of priority queue is:

template<typename Node, typename Cmp = std::less<Node> >
struct deref_compare : std::binary_function<Node*,Node*,bool>
{
    deref_compare(Cmp const& cmp = Cmp())
    : cmp(cmp) {}

    bool operator()(Node* a, Node* b) const {
        return (a->getfValue()> b->getfValue());
    }

private:
    Cmp cmp;
};

typedef deref_compare<Node,std::greater<Node> > my_comparator_t;
priority_queue<Node*,vector<Node*>,my_comparator_t> openq; 

I am doing:

openq.push(myNode)

After entering 3-4 nodes I am getting segmentation fault.

mynode is not empty.

How can i resolve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

茶色山野 2024-08-13 01:41:03

看起来熟悉

不幸的是你搞砸了模板。如果您不需要比较指针的通用解决方案,您也可以编写不带任何模板魔法的函子:

struct my_compare {
    bool operator()(Node const* n1, Node const* n2) const {
        return n1->getfValue() > n1->getfValue();
    }
};

priority_queue<Node*,vector<Node*>,my_compare> foo;

至于您遇到的错误。你没有给我们足够的信息。看来问题不在于您使用的函子。更有可能的是你的其余代码应该受到指责。

我还希望您的节点由其他一些数据结构(例如 std::set)管理(如生命周期管理),并且它们的寿命足够长。否则,很容易犯错误,导致内存泄漏或未定义的行为。请记住,自动对象(位于“堆栈”中的对象)在离开其作用域时会被销毁,并且指向位于另一个容器内的对象的指针在修改容器后可能会变得无效。查看各种容器对迭代器所做的保证以及迭代器何时/是否无效。

Looks familiar.

Unfortunately you screwed up the template. If you don't want a generic solution for comparing pointers you could just as well write your functor without any template magic:

struct my_compare {
    bool operator()(Node const* n1, Node const* n2) const {
        return n1->getfValue() > n1->getfValue();
    }
};

priority_queue<Node*,vector<Node*>,my_compare> foo;

As for the error you're getting. You didn't give us enough information. It seems the problem is not with the functor you used. It's more likely that the rest of your code is to blame.

I also hope you that your nodes are managed (as in life-time management) by some other data structure (like a std::set, for example) and that they live long enough. Otherwise, it's easy to make mistakes that lead to memory leaks or undefined behaviour. Keep in mind that automatic objects (those that live on the "stack") are destroyed when their scope is left and that pointers to objects that live inside another container might become invalid after modifying the container. Check out the guarantees various containers make about iterators and when/if iterators are invalidated.

司马昭之心 2024-08-13 01:41:03

我们需要查看插入节点的代码。听起来好像其中一个节点在插入后被销毁,也许您正在插入一个指向基于堆栈的对象的指针?

另外,这不是问题,但您的 cmp 从未被使用,您总是在进行大于比较。

We need to see the code where you're inserting the nodes. It sounds like one of the nodes is being destroyed after being inserted, perhaps you're inserting a pointer to a stack-based object?

Also, it's not the problem, but your cmp is never being used, you're always doing a greater-than comparison.

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