手寫STL棧功能

棧是一種FILO的數據結構(first in last out),模擬C++中STL中棧的實現
\\genStack.h
#ifndef STACK
#define STACK

#include<vector>
using namespace std;
template<class T,int capaceity = 30>
class Stack{
public:
	Stack(){
		pool.reserve(capaceity);
	}
	void clear(){
		pool.clear();
	}
	bool isEmpty() const {
		return pool.empty();
	}
	T& topEL(){
		return pool.back();
	}
	T pop(){
		T el=pool.back();
		pool.pop_back();
		return el;
	}
	void push(const T& el){
		pool.push_back(el);
	}
private:
	vector <T> pool;
};

#endif
-------------------------
\\Stack.cpp
#include<iostream>
#include<cstdlib>
#include<cstdio>

#include"genStack.h"
using namespace std;

int main()
{
	Stack<int>a;
	a.push(2);
	cout<<a.topEL()<<endl;
	cout<<a.isEmpty()<<endl;
	a.push(1314);
	cout<<a.topEL()<<endl;
	a.pop();
	cout<<a.topEL()<<endl;
	a.clear();
	cout<<a.isEmpty()<<endl;
	cout<<a.topEL();
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章