为什么某些STL容器(堆栈,队列,优先级队列)没有受支持的迭代器?

问题描述 投票:0回答:1

[在所有类型的迭代器中,为什么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个回答
4
投票

这不是经典容器,而是container adaptors。他们不需要支持迭代。引用“ C ++编程语言”这本书:

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

它们是:

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

© www.soinside.com 2019 - 2024. All rights reserved.