STL --> stack栈
2015-06-04 21:04
260 查看
stack栈
c++stack(堆栈)是一个容器的改编,它实现了一个先进后出的数据结构(FILO),使用该容器时需要包含#include<stack>头文件;
定义stack对象示例:
stack的基本操作:
stack的使用范例:
c++stack(堆栈)是一个容器的改编,它实现了一个先进后出的数据结构(FILO),使用该容器时需要包含#include<stack>头文件;
定义stack对象示例:
stack<int>s1; stack<string>s2;
stack的基本操作:
s.empty() 如果栈为空返回true,否则返回false s.size() 返回栈中元素的个数 s.pop() 删除栈顶元素但不返回其值 s.top() 返回栈顶的元素,但不删除该元素 s.push() 在栈顶压入新元素
stack的使用范例:
//栈 stack支持 empty() size() top() push() pop() // by MoreWindows(http://blog.csdn.net/MoreWindows) #include <stack> #include <vector> #include <list> #include <cstdio> using namespace std; int main() { //可以使用list或vector作为栈的容器,默认是使用deque的。 stack<int, list<int>> a; stack<int, vector<int>> b; int i; //压入数据 for (i = 0; i < 10; i++) { a.push(i); b.push(i); } //栈的大小 printf("%d %d\n", a.size(), b.size()); //取栈项数据并将数据弹出栈 while (!a.empty()) { printf("%d ", a.top()); a.pop(); } putchar('\n'); while (!b.empty()) { printf("%d ", b.top()); b.pop(); } putchar('\n'); return 0; }
相关文章推荐
- 利用NLB群集实现WEB站点的高可用部署
- 文件名通配符 “?” “*” “[]” “[!]”
- 反射_类加载器_代理
- P122.42
- JAVA基础——IO流
- 欢迎使用CSDN-markdown编辑器
- Mac OS安装Apache、Tomcat、Nginx、PHP、MySQL、终端配色
- 英语要提高阅读和听力
- 小组自评、互评
- 乂文®便携触摸屏-介绍
- 将博客搬至CSDN
- 对软件功程课的改善建议
- 新的开始
- iOS 7系列译文:忘记NSURLConnection,拥抱NSURLSession吧!
- suspend() 和 resume() 方法,notify()和wait()方法区别
- 如何提升你的能力?给年轻程序员的几条建议
- BZOJ3990 排序
- 【每日scrum】NO.3
- 简单谈一下wordpress中的ajax请求,为什么会一直返回“0”
- 我所理解的设计模式(C++实现)——适配器模式(Adapter Pattern)