类模板 stack

来源:互联网 发布:超星阅读器 mac 编辑:程序博客网 时间:2024/05/16 12:54

很纠结,很郁闷,类模板和类模板的实现需要放在一起,至于原因为何,好像是类模板不是类。。。额,还是放在一起写吧。不然很浪费时间,一种方式能行得通,为何需要寻找另一种方式呢?

模板类stack.h文件(虽然是.h文件,但是该文件中已经包含了对模板类的实现----定义):

/*******************************************************  * 基本操作: * 构造函数:        创建一个新栈(处理动态数据成员的内存分配,并初始化所有数据成员)  * 复制构造函数:    创建stack的一份复制 * 析构函数:        释放构造函数获取的内存 * = :             赋值运算符 * empty():         检查栈是否为空  * push():          通过在栈顶添加一个值来修改栈  * top():           提取栈顶的值,保持栈不被改变  * pop():           通过删除栈顶的值来修改栈  * << 和 display(): 显示所有的栈元素  *  * 类不变式(类不变式简单的意思就是保证类可以正常执行):  * 1. 栈元素(如果有)被存储在myArray的0,1,...,myTop等位置  * 2. -1 <= myTop <= myCapacity  *******************************************************/ #include <iostream>#include <cassert>#include<cstdlib>#ifndef STACK_H#define STACK_Husing namespace std;template <typename T>// 模板函数使用三条规则:// ①. 所有定义在类声明之外的操作必须是模板参数// ②. 使用模板类作为类型:模板类中出现模板类名称的地方,如形参,返回值,复制、构造函数,以及输出操作符。域操作符::前面也要添加// ③. 要包含模板类头文件。class Stack{    public:        Stack(int elementNumber = 128);        // 析构函数        virtual ~Stack();        // copy constructor, 复制构造函数        Stack(const Stack<T>& original);        // assignment 赋值构造函数        const Stack<T>& operator=(const Stack<T>& original);        bool empty() const;        void push(const T& value) ;        void display(ostream & out)const;        T top() const;        void pop();    protected:    private:        int myCapacity,            myTop;        T * myArray;};#endif // STACK_H#include<new>#include<iostream>template<typename T>Stack<T>::Stack(int elemenetNumber){    assert(elemenetNumber>0);    myCapacity = elemenetNumber;        // 1    myArray = new(nothrow) T[elemenetNumber];    if(myArray){        myTop = -1;    }else{        cerr<<"Inadequate memory to allocate...\n";        exit(1);    }}template<typename T>Stack<T>::~Stack(){    delete [] myArray;}template<typename T>Stack<T>::Stack(const Stack<T>& original):myCapacity(original.myCapacity),myTop(original.myTop){    myArray = new (nothrow) T[myCapacity];    if(myArray){        for(int position = 0; position<= myTop; ++position ){            myArray[position]  = original.myArray[position];        }    }else{        cerr << "InAdequate memeory to allocate....\n";        exit(1);    }}template<typename T>const Stack<T> & Stack<T>::operator=(const Stack<T>& rhs){    if (this == &rhs) return *this; // handle self assignment    //assignment operator    if(myCapacity!= rhs.myCapacity)        delete[] myArray;    myArray = new (nothrow)T[rhs.myCapacity];    if(myCapacity==0){        cerr<<"InAdequate memeory to allocate....\n";        exit(1);    }    myCapacity = rhs.myCapacity;    myTop = rhs.myTop;    for(int pos = 0; pos<=myTop; ++pos){        myArray[pos] = rhs.myArray[pos];    }    return *this;}template<typename T>bool Stack<T>::empty() const{    return (myTop == -1);}template<typename T>void Stack<T>::push(const T& value){    if(myTop+1<myCapacity){     // 容量为5,索引最大值为4        myTop++;        myArray[myTop] = value;    }}template<typename T>void Stack<T>::display(ostream & out)const{    for(int pos= myTop;pos>=0; pos--){        out<< myArray[pos] << "\t";    }}template<typename T>T Stack<T>::top() const{    if(!empty()){        return myArray[myTop];    }else{        cerr<<"Stack is empty...\n";        T *temp = new (T);        T garbage = *temp;        delete temp;        return garbage;    }}template<typename T>void Stack<T>::pop(){    if(myTop>=0){        myTop--;    }else{        cerr <<"Stack is empty...\n";    }}template<typename T> ostream &operator<<(ostream &out, const Stack<T> &st){    st.display(out);    return out;}

模板类测试文件:

#include <iostream>#include "Stack.h"using namespace std;template <typename T>void print(Stack<T> st){    st.display(cout);}int main(){    int cap;    cout<< "Enter stack capacity: ";    cin >> cap;    Stack<int> intSt;    Stack<char> charSt;    for(int i=1 ; i<cap; ++i){        intSt.push(100*i);    }    cout<< intSt<<endl;    for(char ch='A'; ch<'D'; ch++){        charSt.push(ch);    }    cout<<charSt<<endl;    cout<<"contents of stack intSt (via print): \n";    print(intSt);    cout<<endl;    Stack<int> i_Stack;    i_Stack = intSt;    cout<<"contents of stack i_Stack after i_stack = intSt(via print):\n";    print(i_Stack);cout<<endl;    cout<<"stack i_stack is empty()?"<<boolalpha<<i_Stack.empty()<<endl;    cout<<"Top value in i_stack"<<i_Stack.top()<<endl;    cout<<"Topping value in i_stack"<<endl;    while(!i_Stack.empty()){        cout<<"\nTopping i_stack"<<i_Stack.top()<<endl;        i_Stack.pop();        cout<<"contents of stack i_stack: "<<i_Stack;    }    cout<<"i_stack is empty? "<< boolalpha << i_Stack.empty()<<endl;    cout<<"Top value of i_stack is: "<<i_Stack.top()<<endl;    cout<<"Another Popping: ";    i_Stack.pop();    return 0;}

程序输出文件:

Enter stack capacity: 5400     300     200     100C       B       Acontents of stack intSt (via print):400     300     200     100contents of stack i_Stack after i_stack = intSt(via print400     300     200     100stack i_stack is empty()?falseTop value in i_stack400Topping value in i_stackTopping i_stack400contents of stack i_stack: 300  200     100Topping i_stack300contents of stack i_stack: 200  100Topping i_stack200contents of stack i_stack: 100Topping i_stack100contents of stack i_stack: i_stack is empty? trueStack is empty...Top value of i_stack is: 137016Another Popping: Stack is empty...Process returned 0 (0x0)   execution time : 2.344 sPress any key to continue.













0 0
原创粉丝点击