c++ - Stack.top() 在访问带有元素的堆栈时给出 bad_access_code

标签 c++ xcode stack

我有一个关于 C++ 堆栈的问题。我尝试访问 stack.top() 但它崩溃了,因为它声明我正在访问一个无效地址。虽然我已经检查以确保堆栈在访问它之前包含元素。

注意:我正在尝试为后缀创建一个中缀,然后计算一个值的后缀。

这里是我的 main.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

//
//  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

请帮助我理解为什么会出现此错误。

最佳答案

为了

std::stack<double /*, std::deque<double>*/> stack; 

stack.top();

effectively calls [container].back(). ,恰好是 std::deque::back在你的情况下,反过来

Calling back on an empty container is undefined.

持有; stack::popdeque::pop_back 也是如此。

因此,您应该检查是否确实将某些东西压入堆栈,如果没有,为什么不,并且当您调用 pop() 时,堆栈上确实有某物要被 pop ped .

关于c++ - Stack.top() 在访问带有元素的堆栈时给出 bad_access_code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33770591/

相关文章:

c++ - Qt:某些插槽不会在 Release模式下执行

c++ - ReleaseSemaphore 不释放信号量

c++ - 如何通过 GDAL 从位于 RAM 中的 geotiff 创建数据集

ios - 配置应用程序时出现 Firebase 错误 - 无法识别的选择器已发送至类

c - 基本堆栈中的内存泄漏

c - 如何通过良好的设计保留堆栈空间?

具有包装随机访问的 C++ vector ?

iphone - 如何陈述 'animation stop animating'的命令?

ios - 警告文本字段中的大写 [仅第一个字]

python - 在 Python 中以前缀表示法处理命令行参数