std::vector 与 std::stack [英] std::vector vs std::stack

查看:62
本文介绍了std::vector 与 std::stack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std::vectorstd::stack 有什么区别?

显然,vectors 可以删除集合中的项目(虽然比列表慢得多),而堆栈被构建为仅 LIFO 集合.

Obviously vectors can delete items within the collection (albeit much slower than list) whereas the stack is built to be a LIFO-only collection.

但是,对于最终项目的操作,堆栈是否更快?是链表还是动态重新分配的数组?

我找不到关于堆栈的太多信息,但如果我正确地描绘了它们(它们类似于实际的线程堆栈;推送、弹出等 - 以及 top() 方法),那么它们似乎非常适合窗口堆叠管理.

I can't find much information about stacks, but if I'm picturing them correctly (they are similar to an actual thread stack; push, pop, etc. - along with that top() method) then they seem perfect for window-stacking management.

推荐答案

stack 不是容器;它是一个容器适配器.它有一个 vectordeque 或类似的容器,它作为一个成员存储,实际保存元素.请记住:它被声明为:

A stack is not a container; it is a container adapter. It has a vector, deque or similar container that it stores as a member that actually holds the elements. Remember: it is declared as:

template<
    class T,
    class Container = std::deque<T>
> class stack;

stack 所做的就是将用户界面限制在这个内部容器中.操作的性能特征完全取决于底层容器的性能特征.

All stack does is limit the user interface to this internal container. The performance characteristics of the operations are exactly whatever the underlying container's performance characteristics are.

这篇关于std::vector 与 std::stack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆