Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Priority queue to find and modify objects

I am trying to implement an A* algorithm and I need a priority queue, but the the std::priority_queue doesn't work for me because I need to find whether an element(a Node object) is in the priority_queue or not, to access its data and to modify it if necessary.

Can I do this using the std::priority_queue somehow?

I would appreciate code suggestions since I don't have much experience with std::priority_queue.

like image 986
mariya Avatar asked Nov 02 '14 14:11

mariya


People also ask

Can we search in priority queue?

Use a priority_queue and another data structure support search i.e. binary search tree , hash . Here I use multimap . Maintain a priority_queue of Node and a multimap of Node at the same time. Then you can get data's pointer by key using multimap d .

How do you access the elements of the priority queue?

Access Element from the Priority Queue We access the top element of the priority_queue using the top() method. In the above example, we have inserted elements into priority_queue in the following order: 1, 20, 7.

How do I change priority priority queue?

To update priority in Priority Queue, get the index of the element that you want to update the priority of and assign a new key to the element. After updating the priority of an element, we need to heapify the heap to maintain the heap data structure.

How do you structure a priority queue?

Begin Define a structure of type student. Initialize variables in student structure. Define another structure of type comparemarks Overload the variables of student structure in comapremarks structure. Use priority queue with structure.


1 Answers

"but the the stl::priority_queue doesn't work for me because I need to find whether an element(a Node object) is in the priority_queue or not, to access its data and to modify it if necessary."

You can well do this for any kind of class providing an appropriate Compare class parameter.

std::priority_queue<T> requires the underlying Container to comply with the concept of a SequenceContainer.

template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
> class priority_queue;

you can take the address of the std::priority_queue<T>::front() reference, and iterate through the queue, to find certain instances.


If you really need to have uniquely existent instances of objects, that should be managed additionally by some priority algorithm, it could be a good idea to store smart pointers (e.g. std::shared_ptr<T>), rather than values or raw pointers. The Compare class needs to be adapted appropriately of course.


struct CompareNodes {
    bool operator
        ( const std::shared_ptr<Node>& lhs
        , const std::shared_ptr<Node>& rhs
        ) {
        // Provide some operation to compare lhs < rhs (less) results in true
        // This function is meant to determine the actual priority of your Node
        // instances, when referenced in the priority_queue<> in question.
    }
};

std::priority_queue
    < std::shared_ptr<Node>
    , std::vector<std::shared_ptr<Node>>
    , CompareNodes
    > myQueue;

"to access its data and to modify it if necessary."

Using the priority queue with std::shared_ptr as shown in the above sample, may also release you from even need to find instances in the queue, and synchronize data modifications from the original instance.

like image 144
πάντα ῥεῖ Avatar answered Oct 07 '22 09:10

πάντα ῥεῖ