user466534
user466534

Reputation:

How do I use a priority queue in c++?

For example we have priority_queue<int> s; which contains some elements. What will be correct form of the following code:

while (!s.empty()) {
    int t=s.pop();// this does not retrieve the value from the queue
    cout<<t<<endl;
}

Upvotes: 0

Views: 874

Answers (1)

GManNickG
GManNickG

Reputation: 504333

Refer to your documentation and you'll see pop has no return value. There are various reasons for this, but that's another topic.

The proper form is:

while (!s.empty())
{
    int t = s.top();
    s.pop();

    cout << t << endl;
}

Or:

for (; !s.empty(); s.pop())
{
    cout << s.top(); << endl;
}

Upvotes: 7

Related Questions