【发布时间】:2020-09-16 05:52:35
【问题描述】:
在所有类型的迭代器中,为什么不支持 stack、queue 和 priority_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;
}
【问题讨论】:
-
支持的模式是什么意思?
-
@rekkalmd 这是因为这些数据结构只能访问顶部元素。
-
阅读What are Containers/Adapters 和std::vector vs std::stack。而且,您始终可以改用std::vector。
-
只有能够访问顶部元素才能使堆栈成为堆栈。