【问题标题】:Why there is no supported iterators for some STL containers (Stack, Queue, Priority Queue)?为什么某些 STL 容器(堆栈、队列、优先级队列)不支持迭代器?
【发布时间】:2020-09-16 05:52:35
【问题描述】:

在所有类型的迭代器中,为什么不支持 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;
}

【问题讨论】:

标签: c++ stl iterator


【解决方案1】:

这三个不是经典的容器,而是container adaptors。他们不需要支持迭代。摘自《C++ 编程语言》一书:

容器适配器提供对底层的特殊访问 容器。

他们是:

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

【讨论】:

    猜你喜欢
    • 2014-10-18
    • 2016-04-13
    • 2018-08-10
    • 2021-08-19
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    相关资源
    最近更新 更多