读书人

什么是模版如何应用模版构造栈还有队

发布时间: 2012-03-30 17:32:09 作者: rapoo

什么是模版,怎么应用模版构造栈还有队列,能用实际代码做例子说明下么
谢谢

[解决办法]

C/C++ code
#include "stdafx.h"#include<iostream>using namespace std;template <typename Type>class TStack{private:    enum {size=100};    Type stack[size];    int top;public:    TStack():top(0){}    void push(const Type& element);    Type pop();};template <typename Type>void TStack<Type>::push(const Type& element){    if (top==size)exit(1);    stack[top++]=element;}template<typename Type>Type TStack<Type>::pop(){    if(top==0)exit(1);    return stack[--top];}int main(){    TStack<int> is;    for(int i=0;i<10;i++)        is.push(i);    for(int k=0;k<10;k++)        cout<<is.pop()<<", ";    TStack<double> ds;    ds.push(3.14);    cout<<ds.pop()<<endl;    return 0;} 

读书人网 >C++

热点推荐