大一新生问幼稚问题
wbgxx 2009-05-25 10:09:08 #include<iostream.h>
#include<stdlib.h>
const int max=20;
template<class T>
class stack
{
T s[max];
int top;
public:
stack()
{top=-1;}
void push(const T &item);
T pop();
int stackempty() const;
};
template<class T>
void stack<T>::push(const T&item)
{
if(top==max-1)
{cout<<"栈已满"<<endl;
exit(1);}
s[top++]=item;
}
template<class T>
T stack<T>::pop()
{
T temp;
if(top==-1)
{cout<<"栈为空,不能出栈炒作:"<<endl;
exit(1);
}
temp=s[top--];
return temp;
}
template<class T>
int stack<T>::stackempty() const
{return top==-1;}
int main()
{
stack<int>s1;
int a[]={4,8,3,2};
cout<<"整数栈"<<endl;
cout<<"如栈顺序:";
for(int i=0;i<4;i++)
{cout<<a[i]<<endl;
s1.push(a[i]);}
cout<<endl<<"出栈顺序:";
while(!s1.stackempty())
{cout<<s1.pop()<<endl;}
stack<char>s2;
int b[]={'a','b','p','d'};
cout<<"字符"<<endl;
cout<<"如栈顺序:"<<" ";
for(int i=0;i<4;i++)
{cout<<b[i]<<endl;
s2.push(b[i]);}
cout<<endl<<"出栈顺序:"<<" ";
while(!s2.stackempty())
{cout<<s2.pop()<<endl;}
}
const T &item 已经被说明为不变的值了为啥还有 s[top++]=item; 这不是改变item的值了吗 为啥啊 ??