按书建栈,编译没通过
我按书上输了一个栈实例,但通不过编译,请教高手:#include<iostream>
#include<string>
#include<vector>
using namespace std;
class istack{
public:
iStack( int capacity )//这里就通不过了
: _stack( capacity ), _top( 0 ) {}
bool pop(int &value);
bool push(int value);
bool full();
bool empty();
void display();
int size();
private:
int _top;
vector<int>_stack;
};
inline bool istack::size(){return _top;};
inline bool istack::empty()
{return _top?false:true;}
inline bool istack:full()
{return _top<_stack.size()-1?false:true;}
bool istack::pop(int &top_value)
{
if(empty())
return false;
top_value=stack[--_top];
cout<<"istack:pop():"<<top_value<<endl;
return true;
}
bool istack::push(value)
{
cout<<"istack::push()("<<value<<")\n";
if(full())
return false;
_stack[_top++]=value;
return true;
}
void istack::display()
{
if(!size())
{cout<<"(0)\n";return;}
cout<<"("<<size()<<")(bot:";
for(int ix=0;ix<_top;++ix)
cout<<_stack[ix]<<" ";
cout<<":top)\n";
}
int main()
{
istack stack(32);
stack.display();
for(int ix=1;ix<51;++ix)
{
if(ix%2==0)stack.push(ix);
if(ix%5==0)stack.display();
if(ix%10==0){
int dummy;
stack.pop(dummy);stack.pop(dummy);
stack.display();
}
}
}
[tk16] [tk16] [tk16] stack[--_top];
??这个哪来的 #include<iostream>
#include<vector>
#include<string>
using namespace std;
class istack
{
public:
istack( int cap ): _capacity( cap ), _top(0) {}
bool pop();
bool push(int value);
bool full();
bool empty();
void display();
int size();
private:
vector<int>::size_type _top;
int _capacity;
vector<int> _stack;
};
inline int istack::size(){return _capacity;};
inline bool istack::empty()
{return _top?false:true;}
inline bool istack::full()
{return _top<size()-1?false:true;}
bool istack::pop()
{
if(empty())
return false;
int i=_stack.at(_top);
_stack.pop_back();
cout<<"istack:pop():"<<i<<endl;
_top--;
return true;
}
bool istack::push(int value)
{
if(full())
return false;
_top++;
_stack.push_back(value);
cout<<"istack::push()("<<value<<")\n";
return true;
}
void istack::display()
{
if(!size())
{cout<<"(0)\n";return;}
cout<<"("<<size()<<")(bot:";
for(vector<int>::size_type ix=_top-1;ix>0;ix--)
cout<<_stack.at(ix)<<" ";
cout<<":top)\n";
}
int main()
{
istack stack(32);
for(int ix=0;ix<stack.size();++ix)
{
stack.push(ix);
}
stack.display();
return 0;
} 栈模板是适配器...一般是用向量和队列做基础构建.. 回答楼上的朋友,这个是c++ primer 3th中文版中的一道题。 再请教下,楼上的朋友写的是没有错的,但里面有一句,我不知道为什么要这样写(因为根本没定义过_stack.at(_top),还有为什么dev c++可以通过编译,而C-FREE不能通过编译。
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class istack
{
public:
istack( int cap ): _capacity( cap ), _top(0) {}
bool pop();
bool push(int value);
bool full();
bool empty();
void display();
int size();
private:
vector<int>::size_type _top;
int _capacity;
vector<int> _stack;
};
inline int istack::size(){return _capacity;};
inline bool istack::empty()
{return _top?false:true;}
inline bool istack::full()
{return _top<size()-1?false:true;}
bool istack::pop()
{
if(empty())
return false;
int i=_stack.at(_top);//就是这一句,为什么可以通过编译呢?at是什么???
_stack.pop_back();
cout<<"istack:pop():"<<i<<endl;
_top--;
return true;
}
bool istack::push(int value)
{
if(full())
return false;
_top++;
_stack.push_back(value);
cout<<"istack::push()("<<value<<")\n";
return true;
}
void istack::display()
{
if(!size())
{cout<<"(0)\n";return;}
cout<<"("<<size()<<")(bot:";
for(vector<int>::size_type ix=_top-1;ix>0;ix--)
cout<<_stack.at(ix)<<" ";
cout<<":top)\n";
}
int main()
{
istack stack(32);
for(int ix=0;ix<stack.size();++ix)
{
stack.push(ix);
}
stack.display();
return 0;
}
[tk11] [tk07] 这个函数是vector的...用法就是把删除的元素显示到屏幕...建议用vc++6.0以上版本
有点小问题改正了
#include<iostream>#include<vector>
#include<string>
using namespace std;
class istack
{
public:
istack( int cap ): _capacity( cap ), _top(0) {}
bool pop();
bool push(int value);
bool full();
bool empty();
void display();
int size();
private:
vector<int>::size_type _top;
int _capacity;
vector<int> _stack;
};
inline int istack::size(){return _capacity;};
inline bool istack::empty()
{return _top?false:true;}
inline bool istack::full()
{return _top<size()-1?false:true;}
bool istack::pop()
{
if(empty())
return false;
int i=_stack.at(_top-1);
_stack.pop_back();
cout<<"istack:pop():"<<i<<endl;
_top--;
return true;
}
bool istack::push(int value)
{
if(full())
return false;
_top++;
_stack.push_back(value);
cout<<"istack::push()("<<value<<")\n";
return true;
}
void istack::display()
{
if(!size())
{cout<<"(0)\n";return;}
cout<<"("<<size()<<")(bot:";
for(vector<int>::size_type ix=_top-1;ix>0;ix--)
cout<<_stack.at(ix)<<" ";
cout<<":top)\n";
}
int main()
{
istack stack(32);
for(int ix=0;ix<stack.size();++ix)
{
stack.push(ix);
}
stack.display();
stack.pop();
stack.pop();
return 0;
} 再次请教楼上的朋友[tk03]
生成的可执行程序有点大(一般都500~600K),怎么能叫他变小一些啊?我感觉这样的程序的话,几十K够了呢。[tk05] 最初的程序已修改至正确了,修改的地方如下所示:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class istack{
public:
iStack( int capacity [color=Red])//这里应该是istack(S的大写不对了)[/color]
: _stack( capacity ), _top( 0 ) {}
bool pop(int &value);
bool push(int value);
bool full();
bool empty();
void display();
int size();
private:
int _top;
vector<int>_stack;
};
inline bool istack::size(){return _top;}[color=Red]//这里的bool应为int[/color]
inline bool istack::empty()
{return _top?false:true;}
inline bool istack:full()[color=Red]//这里的:就为::[/color]
{return _top<_stack.size()-1?false:true;}
bool istack::pop(int &top_value)
{
if(empty())
return false;
top_value=stack[--_top];[color=Red]//这里的stack应为_stack[/color]
cout<<"istack:pop():"<<top_value<<endl;
return true;
}
bool istack::push(value)[color=Red]//这里的value应为int value[/color]
{
cout<<"istack::push()("<<value<<")\n";
if(full())
return false;
_stack[_top++]=value;
return true;
}
void istack::display()
{
if(!size())
{cout<<"(0)\n";return;}
cout<<"("<<size()<<")(bot:";
for(int ix=0;ix<_top;++ix)
cout<<_stack[ix]<<" ";
cout<<":top)\n";
}
int main()
{
istack stack(32);
stack.display();
for(int ix=1;ix<51;++ix)
{
if(ix%2==0)stack.push(ix);
if(ix%5==0)stack.display();
if(ix%10==0){
int dummy;
stack.pop(dummy);stack.pop(dummy);
stack.display();
}
}
[color=Red] //这里应加入system("pause");来暂停一下程序显示(dev c++中不加就一闪而过了)
//这里还应加个return 0,不加也没问题,但是应该养成好习惯的吧:)[/color]
}
[color=Lime]总结一下:看着书打都能犯下这么多的错误,怪自己太粗心大意了。编译没通过也没好好查看一下,怪自己态度不大对啊。虽然说程序现在能运行了,但叫我自己写的话,1天也不见得写得出来,再加油学习吧~
尽是一些问题,导致程序不能运行。。。不知道高手是不是也经常犯这样的问题呢?
[tk03] [tk11] 挺喜欢这小兔子,哈哈~~~[/color]
[[it] 本帖最后由 dubaoshi 于 2008-5-10 09:34 编辑 [/it]] 其实程序是很小的..里面可能用到动态连接库所以文件很大了... 楼上的朋友怎么每天都在啊,呵呵,你是什么工作呢?
页:
[1]
