views:

80

answers:

1

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;
}
+7  A: 

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