模板每次使用前都得重新声明 | #include<iostream>
using namespace std;
template <class T,int n=10>
class anystack
{
T tstack[n];
int element;
int top;
public :
anystack():element(n),top(0) {}
int gettop()
{
return top;
}
bool push(T);
bool pop(T&);
};
template <class T,int n>
bool anystack <T,n>::push(T elem)
{
if(top<=element)
{
tstack[top]=elem;
top++;
return true ;
}
else
return false;
}
template <class T,int n>
bool anystack <T,n>::pop(T&elem)
{
if(top)
{
top--;
elem=tstack[top];
return true;
}
else
false ;
} |
C++类模板-(stack)<template>
最新推荐文章于 2024-05-17 18:45:00 发布
本文介绍了一个通用模板栈类的实现过程,该栈支持泛型类型,并允许指定默认大小。通过使用C++模板,实现了栈的基本操作如压栈(push)、弹栈(pop)及获取栈顶元素等。
439

被折叠的 条评论
为什么被折叠?



