首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于c++ primer中适配器的有关问题

2013-09-17 
关于c++ primer中适配器的问题#include iostream#include string#include stackusing namespace std

关于c++ primer中适配器的问题

#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
stack<char> sexp;
string exp;
cout<<"Enter a expression:"<<endl;
cin>>exp;
string::iterator iter=exp.begin();
while(iter!=exp.end())
{
if(*iter!=')')
sexp.push(*iter);
else
{
while(sexp.top()!='('&&!sexp.empty())
{
sexp.pop();
    }
if(sexp.empty())
cout<<"parentheses are not matched"<<endl;
else
{
sexp.pop();
sexp.push('@');
}
}
++iter;
}
return 0;
}

问题1:当我输入sss)ss为什么会报错?
问题2:为什么不输出"parentheses are not matched"?
我用vs2010编译的! c++ 适配器stack pop
[解决办法]
while(sexp.top()!='('&&!sexp.empty()) 
应该先!sexp.empty()再sexp.top()!='(',否则sexp.top()!='('可能异常
[解决办法]
msdn中stl关于top函数的使用的说明
The priority_queue must be nonempty to apply the member function.
sexp必须非空才能使用sexp.top()这个函数

热点排行