【发布时间】:2020-07-22 21:13:37
【问题描述】:
当实现std::stack时
有几个选项,例如:
// stack with default underlying deque
std::stack< int > intDequeStack;
// stack with underlying vector
std::stack< int, std::vector< int > > intVectorStack;
// stack with underlying list
std::stack< int, std::list< int > > intListStack;
定义std::stack 有哪些优点和缺点?
在不同的容器上,当我从中得到的只是相同的操作“推送、弹出和顶部”?
换句话说:deque 堆栈与向量堆栈和列表堆栈之间有什么区别?为什么我要选择除 deque 以外的任何东西?
【问题讨论】:
-
你的前提有点破。
stack有多个push、pop和top。例如,它有一个成员c,它是底层容器。它是受保护的,所以如果你愿意,你可以随意摆弄它 -
忘记堆栈。向量、列表或双端队列在正常使用中的优势是什么?然后从堆栈的角度考虑它。
-
你让我很好奇,我该如何处理它,我真的应该这样做吗?
标签: c++ stl containers stdstack