STL中stack详解
2016-01-17 22:06
211 查看
stack
Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container.The standard container classes vector, deque and list fulfill these requirements. By default, if no container class is specified for a particular stack class instantiation, the standard container deque is used.
C++ STL 的堆栈泛化是直接通过现有的序列容器来实现的,默认使用双端队列deque的数据结构,当然,可以采用其他线性结构(vector 或 list等),只要提供堆栈的入栈、出栈、栈顶元素访问和判断是否为空的操作即可。由于堆栈的底层使用的是其他容器,因此,堆栈可看做是一种适配器,将一种容器转换为另一种容器(堆栈容器)。
stack
Member functions
构造器constructor
empty
top
push
pop
size
swap
Member functions
构造器(constructor)
Constructs a stack container adaptor object.std::deque<int> mydeque (3,100); // deque with 3 elements std::vector<int> myvector (2,200); // vector with 2 elements std::stack<int> first; // empty stack using deque(default) std::stack<int> second (mydeque); // stack initialized to copy of deque std::stack<int,std::vector<int> > third; // empty stack using vector std::stack<int,std::vector<int> > fourth (myvector);
empty
Returns whether the stack is empty: i.e. whether its size is zero.判断栈是否为空
top
Returns a reference to the top element in the stack.Since stacks are last-in first-out containers, the top element is the last element inserted into the stack.
获取栈顶的元素,根据先入先出原则,获得最后压入栈中的元素
push
Inserts a new element at the top of the stack, above its current top element. The content of this new element is initialized to a copy of val.将一个元素压入栈中,作为一个新的栈顶
pop
Removes the element on top of the stack, effectively reducing its size by one.The element removed is the latest element inserted into the stack, whose value can be retrieved by calling member stack::top.
弹出栈顶元素
// stack::push/pop/empty/pop #include <iostream> // std::cout #include <stack> // std::stack int main () { std::stack<int> mystack; for (int i=0; i<5; ++i) mystack.push(i); std::cout << "Popping out elements..."; while (!mystack.empty()) { std::cout << ' ' << mystack.top(); mystack.pop(); } std::cout << '\n'; return 0; }
Output:
Popping out elements… 4 3 2 1 0
size
Returns the number of elements in the stack.获得栈中元素的个数
swap
Exchanges the contents of the container adaptor (*this) by those of x.交换两个栈
// stack::swap #include <iostream> // std::cout #include <stack> // std::stack int main () { std::stack<int> foo,bar; foo.push (10); foo.push(20); foo.push(30); bar.push (111); bar.push(222); foo.swap(bar); std::cout << "size of foo: " << foo.size() << '\n'; std::cout << "size of bar: " << bar.size() << '\n'; return 0; }
相关文章推荐
- JavaWeb学习笔记(一)---Web相关知识和HTTP协议
- 配置临时环境变量
- FZUOJ 2150 Problem 2150 Fire Game (双起点BFS)
- CMD运行java class文件
- centos7环境下jdk+tomcat+jenkins+svn安装
- VS2013写window服务,实现定时短信功能
- 参赛蓝桥(新手推荐阅读)
- 2016第2周日
- 問題排查:建立選單時的錯誤 errcode:65318,errmsg:must use utf-8 charset hint: [Vwda70520vr18]
- Linux学习笔记:MySQL的授权
- win10系统安装oracle11g时遇到INS-13001环境不满足最低要求
- handler相关
- CAA开发创建workshop/workbench应用程序的详细步骤(1)
- JavaScript之对象序列化详解
- PHP标准 —— 框架的互操作性
- 应用程序之间的跳转设置info.plist
- 算法导论 Exercises 9.3-9
- BZOJ Tyvj 1729 文艺平衡树
- C#窗体自适应
- C#中的泛型