简体   繁体   中英

How to use the push() of priority_queue?

This is what I have

priority_queue<int, vector<string>, std::greater<int>> Jobs1;

How can I do a push() ?

vector<string> temp;
// Jobs1.push(what goes in here?) <---- I want to push temp in this min heap, and let's say my compare value is 3

Thanks!

As the other answers have said, the priority_queue - like other container types - holds individual items of a particular type.

Your question and additional comments suggest you want to store a bunch of strings, sorted by some arbitrary priority number. To do this, you need to create your own type which includes both the string and its priority. If you provide your own operator< and operator> overloads, then the standard comparison functions will work, too.

Here's a simple example of one way to implement such a class, and how you could use it. This uses C++11 features such as extended initializer lists and auto.

#include <iostream>
#include <queue>
#include <string>

class PriorityString
{
public:
        int priority;
        std::string value;

        PriorityString(int _priority, std::string const stringvalue)
        : priority    (_priority)
        , value       (stringvalue)
        {
        }

        bool operator< (PriorityString const& other) const
        {
                return priority < other.priority;
        }

        bool operator> (PriorityString const& other) const
        {
                return priority > other.priority;
        }
};

int
main()
{
        std::priority_queue< PriorityString, std::vector<PriorityString>, std::less<PriorityString> > strings;

        strings.push( {1, "Alice"} );
        strings.push( {2, "Bob"} );
        strings.push( {4, "Charlie"} );
        strings.push( {3, "Dianne"} );

        while (! strings.empty() )
        {
                auto const& ps = strings.top();
                std::cout << "Priority: " << ps.priority << "  V: " << ps.value << std::endl;
                strings.pop();
        }
}

This will output:

Priority: 4  V: Charlie
Priority: 3  V: Dianne
Priority: 2  V: Bob
Priority: 1  V: Alice

If you exchange the std::less for std::greater then they'll print out in ascending order of priority (ie lower priorities will go to the front of the queue).

If you don't have access to C++11 features, adding items can be done with the explicit constructor:

strings.push( PriorityString(1, "Alice") );

and the return type of strings.top() is of course a constant reference to a PriorityString .

Depending on exactly what you're trying to achieve, you might also want to look into the STL's map and set types, and their unordered variants. A setup like this "PriorityString" can be useful if you always want to process items in a fixed order, and can tolerate (potentially) lengthy operations when adding new items in order to determine where they belong in the order.

The underlying container should store int , not string .

vector<string> should really be vector<int>

#include <iostream>
#include <queue> 

using namespace std;

int main()
{    
    priority_queue<int, vector<int>, std::greater<int>> Jobs1;
    Jobs1.push(3);
    Jobs1.push(4);
    Jobs1.push(5);
    //..    
}

push() is used to insert an element into the priority queue. So, to insert an int into your priority queue, use:

int a = 5;
Jobs1.push(a);

Also, since your priority queue is for integers, the underlying container should be a vector<int> instead of vector<string>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM