例程调用了deque(stack默认underlying data structure),vector, list三个first class containers来描述adapter stack, 并对不同数据结构的stack进行了分别的基本操作。
/*
Tranverse postOrder binary tree
pesudocodes
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <deque>
#include <stack>
using std::stack;
using std::vector;
using std::list;
using std::cout;
using std::endl;
template <typename T> void pushElements(T &stackRef);
template <typename T> void popElements(T &stackRef);
int main(){
//默认deque
stack<int> dequeTypeItem;
//初始化数据结构为vector存储数据
stack<int, vector<int>> vectorTypeItem;
//初始化数据结构为list存储数据
stack<int, list<int>> listTypeItem;
cout<<"For deque: "<<endl;
pushElements(dequeTypeItem);
cout<<"For vector: "<<endl;
pushElements(vectorTypeItem);
cout<<"For list: "<<endl;
pushElements(listTypeItem);
cout<<"Output deque: "<<endl;
popElements(dequeTypeItem);
cout<<"Output vector: "<<endl;
popElements(vectorTypeItem);
cout<<"Output List: "<<endl;
popElements(listTypeItem);
}
//利用stack中的push,top函数进行数据输入
template <typename T>
void pushElements(T &stackRef) {
for (int i=0; i<10; ++i) {
stackRef.push(i);
cout<<stackRef.top()<<" ";
}
cout<<endl;
}
//利用stack中的empty()检查是否为空栈,top,pop对数据进行输出和删除
template <typename T>
void popElements(T &stackRef) {
while (!stackRef.empty()) {
cout<<stackRef.top()<<" ";
stackRef.pop();
}
cout<<endl;
}