在抛出 what() 的实例后终止调用'std::out_of_range':basic_string::at
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at
使用命名空间std;
class stack
{
char data[30];
int top;
public:
void init()
{
top=-1;
}
int is_empty();
void push(char);
char pop();
int precedence(char);
};
int stack::is_empty()
{
if(top==-1)
return(1);
return 0;
}
void stack::push(char op)
{
top++;
data[top]=op;
}
char stack::pop()
{
char x;
x=data[top];
top--;
return(x);
}
int stack::precedence(char op)
{
if(op=='(')
return(1);
if(op=='+' || op=='-')
return(2);
if(op=='*' || op=='/' || op=='%')
return(3);
if(op=='^')
return(4);
return(5);
}
int main()
{
stack s;
string iexp,pexp;
int i,j;
j=0;
char op;
s.init();
cout<<"n enter an infix expr.: ";
cin>>iexp;
for(i=0;i<iexp.length();i++)
{
if(isalpha(iexp.at(i)))
{
pexp.at(j)=iexp.at(i);
j++;
}
else
{
if(s.is_empty())
{
s.push(iexp.at(i));
}
else
{
if(iexp.at(i)=='(')
{
s.push(iexp.at(i));
}
if(iexp.at(i)==')')
{
while((op=s.pop())!='(')
{
pexp.at(j)=op;
j++;
}
}
while(s.precedence(iexp.at(i))<=s.precedence((op=s.pop())))
{
pexp.at(j)=op;
j++;
}`
s.push(iexp.at(i));
}
}
``}
while(!s.is_empty())
{
op=s.pop();
pexp.at(j)=op;
j++;
}
pexp.at(j)=' ';
cout<<"n postfix expr. is : t"<<pexp;
return 0;
}
我是编程新手。我正在进行中缀到后缀的转换,但遇到了错误:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at
所以请帮我解决这个问题。
我不认为您可以使用附加到字符串
pexp.at(j) = ...;
尝试使用
pexp += ...;
相反(在所有附加到pexp的地方(
相关文章:
- 为什么我的向量::擦除调用会抛出"vector subscript out of range"?
- 解决方案在第 25 行执行错误'out of bounds'
- C++ 向量加减抛出"expression: vector subscript out of range."错误
- 从 MSVC14 切换到 MSVC16 会导致"compiler is out of heap space (C1060)"错误
- 庞大的初始化列表,如何修复"fatal error C1060: compiler is out of heap space"
- 如何解决'vector subscript out of range'错误?
- 在析构函数中调用"delete"运算符时"compiler is out of heap space"编译器错误
- 空集"Out of bound iterator"
- 在 std::unordered_map 中插入新的键/值对会导致"out of range"异常
- 注册 Clang 检查器时出错:"out-of-line-definition of register"
- QList::operator[]中的断言失败<T>:QJoysticks 中的"index out of range"
- 尝试获取矩阵的正确对角线会导致"vector out of range"错误
- 运算符方法和返回对象"out-of-scope"?
- 如何使用 mpi 分散修复"vector subscript out of range"?
- 如何修复邻接列表中的"Debug Assertion Failed" "vector subscript out of range"
- "Vector subscript out of range",在返回声明?
- QList<T>::operator[]: "index out of range" 中的断言失败
- 多次调用存储过程时C++连接器"Commands out of sync" mySQL
- 如何处理C++'index out of bounds error'?
- MySQL 异常"connection lost during query"、"MySQL server has gone away"和"command out of sync"