简体   繁体   English

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

[英]Why there is no supported iterators for some STL containers (Stack, Queue, Priority Queue)?

Across all types of iterators, why there is no supported pattern for stack , queue and priority_queue STL containers?在所有类型的迭代器中,为什么不支持stackqueuepriority_queue STL 容器的模式?

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

The three are not classic containers, but rather container adaptors .这三个都不是经典的容器,而是容器适配器 They do not need to support the iteration.他们不需要支持迭代。 A quote from the "C++ Programming Language" book:引用“C++ 编程语言”一书中的一句话:

Container adaptors provide specialized access to underlying containers.容器适配器提供对底层容器的专门访问。

They are:他们是:

intended to be used only through their specialized interfaces.旨在仅通过其专用接口使用。 In particular, the STL container adaptors do not offer direct access to their underlying container.特别是,STL 容器适配器不提供对其底层容器的直接访问。 They do not offer iterators or subscripting.他们不提供迭代器或下标。

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

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