Stack.top() 在访问包含元素的堆栈时提供bad_access_code
Stack.top() giving bad_access_code while accessing a stack with elements
我有一个关于 C++ 堆栈的问题。我尝试访问 stack.top((,但它崩溃了,因为它指出我正在访问一个无效的地址。虽然我已经检查以确保堆栈在访问它之前包含元素。
注意:我正在尝试创建一个后缀的中缀,然后计算一个值的后缀。
这是我的主要.cpp
//
// main.cpp
// testing
//
// Created by Tan Der Shen on 11/17/15.
// Copyright (c) 2015 Tan Der Shen. All rights reserved.
//
#include <iostream>
#include "main.h"
#include <vector>
#include <string>
using namespace std;
int main() {
// insert code here...
std::string st1 = "2 * y + x * (8 + 3) - y";
std::string newstring;
for (int k = 0; k < st1.size(); k++) {
if (st1[k] == '(') {
newstring = newstring + "( ";
}else if (st1[k] == ')') {
newstring = newstring + " )";
}
else {
newstring = newstring + st1[k];
}
}
cout<< newstring;
cout<< "n";
vector<string> test1 = tokenize(newstring, " ");
for (int i = 0; i < test1.size(); i++) {
cout<< test1.at(i);
cout<< "n";
cout<< whatIsIt(test1.at(i));
cout<< "n";
}
for (int j = 0; j < test1.size(); j++) {
if (operators.size() != 0) {
cout<< operators.top();
}
if (postfix.size() != 0) {
cout<< postfix.back();
}
switch (whatIsIt(test1.at(j))) {
case 0:
postfix.push_back("1");
cout<< "done";
break;
case 1:
postfix.push_back(test1.at(j));
break;
case 2:
operators.push(test1.at(j));
break;
case 3:
while (operators.top() != "(") {
postfix.push_back(operators.top());
operators.pop();
}
operators.pop();
break;
case 4:
if (operators.size() == 0) {
operators.push(test1.at(j));
} else {
while (whatIsIt(operators.top()) > 4) {
postfix.push_back(operators.top());
operators.pop();
}
operators.push(test1.at(j));
}
break;
case 5:
if (operators.size() == 0) {
operators.push(test1.at(j));
} else {
while (whatIsIt(operators.top()) > 5) {
postfix.push_back(operators.top());
operators.pop();
}
}
operators.push(test1.at(j));
break;
case 6:
operators.push(test1.at(j));
break;
default:
break;
}
}
while (operators.size() != 0) {
postfix.push_back(operators.top());
operators.pop();
}
for (int v = 0; v < postfix.size(); v++) {
cout<< postfix.at(v);
cout<< " ";
}
cout<< "n";
for (int n = 0; n < postfix.size(); n++) {
cout<< "whatisit: " << whatIsIt(postfix.at(n));
cout<< "n";
switch (whatIsIt(postfix.at(n))) {
case 1:
answer.push(stod(postfix.at(n)));
cout<< "postfix(n): " << postfix.at(n);
cout<< "n";
break;
case 4:
cout<< postfix.at(n);
cout<< "n";
val1 = answer.top();
cout<< "val1: " << val1;
cout<< "n";
answer.pop();
val2 = answer.top();
cout<< "val2: " << val2;
cout<< "n";
answer.pop();
if (postfix.at(n) == "+") {
aVal = val1 + val2;
cout<< "aVal(+): " << aVal;
cout<< "n";
} else {
aVal = val2 - val1;
cout<< "aVal(-): " << aVal;
cout<< "n";
}
answer.push(aVal);
break;
case 5:
cout<< postfix.at(n);
cout<< "n";
val1 = answer.top();
answer.pop();
cout<< "val1: " << val1;
cout<< "n";
val2 = answer.top();
answer.pop();
cout<< "val2: " << val2;
cout<< "n";
aVal = (val2)/(val1);
answer.push(aVal);
cout<< "aVal(/): " << aVal;
cout<< "n";
break;
case 6:
cout<< postfix.at(n);
cout<< "n";
val1 = answer.top();
answer.pop();
cout<< "val1: " << val1;
cout<< "n";
val2 = answer.top();
answer.pop();
cout<< "val2: " << val2;
cout<< "n";
aVal = (val2)*(val1);
answer.push(aVal);
cout<< "aVal(*): " << aVal;
cout<< "n";
break;
default:
break;
}
}
cout<< answer.top();
cout<< "n";
return 0;
}
vector<string>& tokenize(string line, string delimiter){
vector<string>* allSplits = new vector<string>;
string lineCopy = line;
while(lineCopy.size() != 0){
int location = lineCopy.find(delimiter);
if(location == string::npos){
allSplits->push_back(lineCopy);
break;
}
allSplits->push_back(lineCopy.substr(0, location));
lineCopy.erase(0,location + delimiter.size());
}
return *allSplits;
}
int whatIsIt(string& inputOp) {
if (isNumber(inputOp) == 1) {
return 1;
} else if (inputOp == "(") {
return 2;
} else if (inputOp == ")") {
return 3;
} else if (inputOp == "+") {
return 4;
} else if (inputOp == "-") {
return 4;
} else if (inputOp == "/") {
return 5;
} else if (inputOp == "*") {
return 6;
} else {
return 0;
}
}
int isNumber(const string& input){
if(input.size() == 0){
return -1;
}
bool foundDecimal = false;
for(int i=0; i<input.size(); i++){
if(!isdigit(input.at(i))){
if(input.at(i) == '.'){
if(foundDecimal){
return 0;
}else{
foundDecimal = true;
// allows trailing and leading decimal. ex: .1, 10.
}
}else{
return 0;
}
}
}
return 1;
}
这是我的主要。
//
// main.h
// testing
//
// Created by Tan Der Shen on 11/17/15.
// Copyright (c) 2015 Tan Der Shen. All rights reserved.
//
#ifndef testing_main_h
#define testing_main_h
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <stack>
std::vector<std::string>& tokenize(std::string line, std::string delimiter);
int whatIsIt(std::string& inputOp);
int isNumber(const std::string&);
std::vector<std::string> postfix;
std::stack<std::string> operators;
std::stack<double> answer;
double val1;
double val2;
double aVal;
#endif
请帮助我理解为什么我会收到此错误。
for
std::stack<double /*, std::deque<double>*/> stack;
stack.top();
有效地调用 [container].back((.,这恰好在您的情况下std::deque::back
,反过来又
未定义对空容器的回调。
持有; stack::pop
和deque::pop_back
相同。
因此,您应该检查是否真的将某些内容推送到堆栈上,如果没有,为什么不这样做,以及当您调用pop()
堆栈上实际上有要pop
ped 的 sth 时。
相关文章:
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- 为什么调试器引发"read access violation. this was nullptr"异常?
- 在 Microsoft Access SQL 中调用自定义 DLL 函数时传递的内存地址无效
- 使用来自 Excel VBA 的 C++ dll 时"Bad DLL calling convention" - 如何解决?
- Simulink "Access Violation"写入 C++ lambda 函数捕获列表中的 PWork 变量
- Qt QXmlStreamReader Access Violation
- "Access is Denied" U盘上的创建文件()
- 指向 std::unrodered_map 中元素的指针返回'Read access violation'
- C++17 编解码器在将标准::字符串转换为标准::字符串时抛出"bad conversion"
- 例外:'Access violation reading location'
- 向量数组"Cannot access memory at address"
- Windeployqt不会运行,"Access is denied."
- 对于代理容器上的迭代器来说,"least bad implementation"可能是什么?
- 我在发布模式下运行时收到"Access violation reading location"错误 - C++
- Access C++ Enum from Swift
- Java 本机访问代码错误:"Invalid memory access"
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc
- 如何获得私人会员的"read access"?
- 将数据添加到 CArray 会产生错误"cannot access private member declared in class 'CObject'"
- Bad Linux Memory Map File Performance with Random Access C++ & Python