大一新生问幼稚问题

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的值了吗 为啥啊 ??



...全文
144 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
T技术沙龙 2009-05-25
  • 打赏
  • 举报
回复
呵呵,问题不幼稚,只是楼主一时没发现而已,下次仔细考虑就好了
Sou2012 2009-05-25
  • 打赏
  • 举报
回复
UP...
14154 2009-05-25
  • 打赏
  • 举报
回复
这里没有修改item的值,修改的是左值
pengzhixi 2009-05-25
  • 打赏
  • 举报
回复
s[top++]=item;//这里没改变item的值,只是将item的值赋给s[top]
wbgxx 2009-05-25
  • 打赏
  • 举报
回复
哦 !!看来是看书看傻了 !不好意思啊 !
ztenv 版主 2009-05-25
  • 打赏
  • 举报
回复
s[top++]=item; 你修改了item吗?这句里item是右值!!你可以在你这句后面把item做为左值来试试;
tfzhy 2009-05-25
  • 打赏
  • 举报
回复
晕,s[top++]=item是右边值赋值给左边啊 怎么是改变item的值?
lingyin55 2009-05-25
  • 打赏
  • 举报
回复
.[Quote=引用 3 楼 wbgxx 的回复:]
哦 !!看来是看书看傻了 !不好意思啊 !
[/Quote]
xinghuanonline 2009-05-25
  • 打赏
  • 举报
回复
有些算法是从右向左看的

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧