Re-inserting into std::priority_queue

closed account (10oTURfi)
I sometimes need to change the place of the element in the priority_queue. I did it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <queue>
#include <algorithm>

struct Vector2i
{
    unsigned x;
    unsigned y;
    // bla bla
};

struct PathfinderNode
{
    PathfinderNode* pParent;
    Vector2i Position;
    unsigned G, H;
};

struct CompareNode : public std::binary_function<PathfinderNode*, PathfinderNode*, bool>
{
    bool operator()(const PathfinderNode* first, const PathfinderNode* second) const
    {
        return((first->G + first->H) > (second->G + second->H));
    }
};

class my_priority_queue :
    public std::priority_queue<PathfinderNode*, std::vector<PathfinderNode*>, CompareNode>
{
public:
    void re_insert(PathfinderNode* what)
    {
        this->c.erase(std::find(this->c.begin(), this->c.end(), what));
        std::make_heap(this->c.begin(), this->c.end(), CompareNode());
        this->push(what);
    }

    void clear()
    {
        this->c.clear();
    }
    
    void reserve(size_t elements)
    {
        this->c.reserve(elements);
    }
};


Now... It is really hard to tell if it works or not (Cause the algorithm which uses this somehow works without std::make_heap call, so I am not sure if it is correct. Is it? Or is there a better way?
Last edited on
¿what is `this->c' ?

You are just putting the new node at the back, as long as that's its place it would be fine.
You could just `make_heap()' without removing that node.
closed account (10oTURfi)
this->c is std::vector which is used by std::priority queue. Works on gcc and msvc++.

Oh yeah, good idea...That *how did i miss that* feeling
Last edited on
Topic archived. No new replies allowed.