STL容器之stack
来源:互联网 发布:未注册域名查询软件 编辑:程序博客网 时间:2024/05/16 11:23
先说明一个概念,适配器(adapter),适配器是标准库中的通用概念,包括容器适配器、迭代器适配器、函数适配器。本质上,适配器是使一事物的行为类似于另一事物的行为的一种机制。
容器适配器让一种已存在的容器类型以一种不同的抽象类型的工作方式实现。
STL提供了三种顺序容器适配器,queue、priority_queue、stack。
每一种适配器都定义了两个构造函数,如下:
A a;//创建空适配器a
A a(c);//用一个容器c初始化适配器a
默认的stack和queue都基于deque实现,priority_queue基于vector实现。
创建适配器时通过将一个顺序容器指定为适配器的第二个类型实参,可覆盖其关联的基础容器类型,例如:
stack< string, vector<string> > str_stk;
stack可以建立在vector,list或者deque上。
queue要求基础容器提供push_front操作,所以不能建立在vector上。
priority_queue要求提供随机访问,所以不能建立在list之上。
书归正传,先说说stack提供的操作,如下:
empty() 堆栈为空则返回真
pop() 移除栈顶元素,只有pop删除元素
push() 在栈顶增加元素
size() 返回栈中元素数目
top() 返回栈顶元素
下面给出stack的操作使用范例:
#include <iostream>#include <vector>#include <stack>using namespace std;int main(){// 下面程序摘自《c++ primer》中文第四版301页,本例短小精辟,用到了所有操作,所以就不自己写了// number of elements we'll put in our stackconst stack<int>::size_type stk_size = 10;stack<int> intStack;// fill up the stackint ix = 0;while(intStack.size() != stk_size)// use postfix increment;want to push old value onto intStackintStack.push(ix++);// intStack holds 0...9 inclusiveint err_cnt = 0;// look at the top element of the stackwhile(intStack.empty == false){int value = intStack.top();// read the top element of stackif(value != --ix){cerr << "oops! expected " << ix << " received " << value << endl;++err_cnt;}intStack.pop();// pop the top element, and repeat}cout << "Our program ran with " << err_cnt << " errors" << endl;return 0;}
- STL容器之stack
- STL容器之stack
- stl之stack容器
- STL容器之stack
- STL之stack容器
- STL容器之stack栈
- STL之stack&queue容器(笔记)
- STL之容器适配器:stack,queue , priority_queue
- STL之queue和stack容器
- STL之queue和stack容器
- STL学习笔记之容器--stack
- STL容器之stack和queue
- STL容器总结之stack和queue
- STL容器总结之stack和queue
- STL容器适配器:stack
- STL stack容器
- STL容器stack栈
- STL stack堆栈容器
- PHP关于VC9和VC6以及Thread Safe和Non Thread Safe版本选择的问题
- STM8_interrupt_vector学习_1
- sqlserver2008实现拼音首字母和随机n位数的生成
- struts2 自定义标签 实例
- 259. Let the cat of the bag. 泄漏天机
- STL容器之stack
- 第一篇博客
- JS新API标准 地理定位(navigator.geolocation)
- struts2+jquery+ajax(json)使用
- 窗口常用函数
- 贝叶斯分类器----分类器思想的起源(spring)
- 应用学习:VOIP语音通讯类
- 徒弟离职引发的思考
- PLSQL本地不安装客户端远程连接Oracle