简体   繁体   English

priority_queue的基于范围的for循环

[英]Range based for-loop for priority_queue

Defining my priority_queue like this, 像这样定义我的priority_queue,

priority_queue<int> parts(start, start+N, less<int>());

the following code won't compile 以下代码无法编译

for(int t : parts){
    ...
}

Which leads me to question: 这使我产生疑问:

In C++11, are range based for loops allowed for std::priority_queue ? 在C ++ 11中, std::priority_queue是否允许基于范围的循环

In general, which structures are allowed to be iterated trough using a range based for-loop? 通常,允许使用基于范围的for循环迭代哪些结构?

I know I can do pretty much the same thing like this: 我知道我可以做几乎一样的事情:

while(!parts.empty()){
    cout << "Next element: " << parts.top() << endl;
    parts.pop();
}

Is it possible to iterate trough the queue nevertheless? 是否可以通过队列进行迭代?

No, std::priority_queue does not support the range-based for loop. 否, std::priority_queue不支持基于范围的for循环。

The range-based for loop works on arrays and on classes that have begin() and end() member functions. 基于范围的for循环适用于具有begin()end()成员函数的数组和类。 This includes all containers in the C++ standard library as well as std::string (and its basic_string cousins) but not stacks, queues, or priority queues which are container adaptors and do not expose iterators. 这包括C ++标准库中的所有容器以及std::string (及其basic_string cousins),但不包括作为容器适配器且不公开迭代器的堆栈,队列或优先级队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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