为什么某些 STL 容器(堆栈、队列、优先级队列)不支持迭代器?

rek*_*lmd 0 c++ iterator stl

在所有类型的迭代器中,为什么没有支持stack,queuepriority_queueSTL 容器的模式?

#include <iostream>
#include <stack>
#include <algorithm>

int main(){

    std::stack<int> values;
    std::stack<int> valuesCopy;

    values.push(98);
    values.push(11);
    values.push(14);
    values.push(17);
    values.push(20);

    std::for_each( /* How can i manage this in a alternative way */,
                      [&](int value) mutable throw() -> void{ /* Process */ ;} );

    std::copy(/* Same for this */,std::back_inserter(valuesCopy));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Ron*_*Ron 6

这三个都不是经典的容器,而是容器适配器。他们不需要支持迭代。《C++ 编程语言》一书中的引述:

容器适配器提供对底层容器的专门访问。

他们是:

旨在仅通过其专用接口使用。特别是,STL 容器适配器不提供对其底层容器的直接访问。他们不提供迭代器或下标。