开发者

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

开发者 https://www.devze.com 2023-01-07 18:08 出处:网络
For example we have priority_queue<int> s; which contains some elements. What will be correct form of the following code:

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;
}


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消