0

例如我们有priority_queue<int> s;其中包含一些元素。以下代码的正确形式是什么:

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

1 回答 1

7

请参阅您的文档,您会看到pop没有返回值。这有各种原因,但这是另一个话题。

正确的形式是:

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

    cout << t << endl;
}

或者:

for (; !s.empty(); s.pop())
{
    cout << s.top(); << endl;
}
于 2010-07-15T20:29:09.950 回答