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

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

本文关键字:队列 优先级 不支持 迭代器 堆栈 STL 容器 为什么      更新时间:2023-10-16

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

这三个不是经典容器,而是容器适配器。它们不需要支持迭代。引用"C++编程语言"一书中的一句话:

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

它们是:

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