标签:bad vim abs odm xid bs4 str aaaaa xhtml
栈:(stack.h) #ifndef __STACK_H__ #define __STACK_H__ #include <iostream> #include<string> #include<sstream> using namespace std; template <typename T,int num> class Stack { private: int _top; T _parr[num]; public: Stack(); ~Stack(); bool full(); bool empty(); bool push(T elem); bool pop(T &); int getPos() { return _top; } }; template <typename T,int num> Stack<T,num>::Stack():_top(-1) {} template <typename T,int num> Stack<T,num>::~Stack() {} template <typename T,int num> bool Stack<T,num>::full() { return _top == (num-1); } template <typename T,int num> bool Stack<T,num>::empty() { return _top == -1; } template <typename T,int num> bool Stack<T,num>::push(T elem) { if(!full()) { _parr[++_top] = elem; return true; } return false; } template <typename T,int num> bool Stack<T,num>::pop(T & t) { if(!empty()) { t = _parr[_top--]; return true; } else return false; } #endif | 测试代码(testStack.cpp) #include"stack.h" int test0(void) { Stack<int, 10> stackInt; cout << "开始时stakcInt是否为空?" << stackInt.empty() << endl; stackInt.push(5); cout << "此始时stakcInt是否为空?" << stackInt.empty() << endl; for(int idx = 1; idx !=10; ++idx) { stackInt.push(idx); } cout << "此时stakcInt是否已满?" << stackInt.full() << endl; for(int idx = 0; idx != 10; ++idx) { int elem = 0; stackInt.pop(elem); cout << elem << " "; } cout << endl; return 0; } int test1(void) { Stack<string, 10> stackInt; cout << "开始时stakcInt是否为空?" << stackInt.empty() << endl; stackInt.push("aa"); cout << "此始时stakcInt是否为空?" << stackInt.empty() << endl; for(int idx = 1; idx !=10; ++idx) { string s(2, 'a' + idx); //string类的一个构造函数,表示含有2个元素的string对象,其中每个元素都初始化为后面的字符 stackInt.push(s); } cout << "此时stakcInt是否已满?" << stackInt.full() << endl; for(int idx = 0; idx != 10; ++idx) { string elem; stackInt.pop(elem); cout << elem << " "; } cout << endl; return 0; } int main() { test0(); test1(); return 0; } |
标签:bad vim abs odm xid bs4 str aaaaa xhtml
原文地址:https://www.cnblogs.com/meihao1203/p/9190105.html