C ++:Deque Iterator不可解除引用;找不到流氓pop / top

时间:2016-04-06 13:56:36

标签: c++ stack deque

我正在做一些与此用户非常相似的事情: Error: deque iterator not dereferenceable

我一直在寻找一个年龄,但我无法看到我出错的地方。另一张海报的解决方案是找到一个地方,他试图从一个零元素的双端队列弹出或顶部。我无法在我的代码中找到我在做的地方。

编辑:我怀疑问题出在SYAlg或OSProcess中,如果有帮助的话。

// TestCalculator.cpp : main project file.

#include <string>
#include <iostream>
#include <locale>
#include <ctype.h>
#include <vector>
#include <deque>

using namespace System;
using namespace std;
//using std::string;

bool lastCharDigit = true;
string RawString; //Contains the raw equation the user types in.

deque<string> TokenEquation(1); //Contains the equation in tokenised infix form.
deque<string> RPNEquation; //Contains the equation in tokenised RPN form.
deque<string> OperatorStack; //Used as part of the Shunting Yard Algorithm
deque<string> SolverStack; //Used to solve the RPN Equation.

locale loc; //Used to verify digits.
//START FUNCTION DECLARATION
int main();
void tokeniser(string RawEquation);
void SYAlg();
void OSProcess(string newOperator);
void Solver();
//END FUNCTION DECLARATION


int main()
{
    cout << "Please enter a valid infix notation equation, without parenthesis.\n";
    cin >> RawString;
    tokeniser(RawString);
    cout << "\n";
    system("pause");
    return 0;
}

void tokeniser(string RawEquation)
{
    int testCharPos = -1; // Initialise the index of the raw string
    int tokenVectorPos = 0; // Initialise the token array position
    int tokenVectorPrintPos = 0; // Initialise the print position

    for (int eLength = RawEquation.length(); eLength != 0; eLength--) // For each character in the Raw string...
    {
        testCharPos++; // Increment the char we're testing
        char testChar = RawEquation.at(testCharPos); // Establish the current test char

        if (isdigit(testChar, loc)) //If the testchar is a digit
        {
            if (lastCharDigit) //If the last character was a digit
            {
                TokenEquation[tokenVectorPos] += testChar; //Append the tested char to the current token array pos
            }
            if (!lastCharDigit) //If the last character was not a digit
            {
                TokenEquation.push_back(string(1, testChar)); //Establish a new element with the testchar in it.
                tokenVectorPos++;
            }
            lastCharDigit = true;
        }

        if (!isdigit(testChar, loc))//If the testchar is not a digit
        {
            TokenEquation.push_back(string(1, testChar)); //Establish a new element with the testchar in it.
            tokenVectorPos++;
            lastCharDigit = false;
        }
    }

    cout << "The tokens of that equation are:\n\n"; //Outputs the tokens for testing purposes.

    for (int tokenLength = TokenEquation.size(); tokenLength != 0; tokenLength--)
    {
        cout << "     " << TokenEquation[tokenVectorPrintPos];
        cout << "\n";
        tokenVectorPrintPos++;
    }

    SYAlg(); //Call the SYAlg.
}

void SYAlg() //This function uses Shunting Yard Algorithm to convert the Infix tokens to RPN.
{
    cout << TokenEquation.size();
    for (int testtokenLength = TokenEquation.size(); testtokenLength != 0; testtokenLength--) //For each token in the tokenised deque
    {
        if (isdigit(TokenEquation.front().at(0), loc)) //Check if it's a number
        {
            RPNEquation.push_back(TokenEquation.front()); //Add the first raw token to the RPN Equation
            TokenEquation.pop_front(); //Pop the token from the deque
        }
        if (!isdigit(TokenEquation.front().at(0), loc)) //If it's an operator
        {
            OSProcess(TokenEquation.front()); //Run the SYAlg operator stack procedure. NB This will pop the front of the TokenEquation for you.
        }
    }


    cout << "The tokens of that equation are:\n\n"; //Outputs the tokens for testing purposes.
    int RPNPrintPos = 0;
    for (int tokenLength = RPNEquation.size(); tokenLength != 0; tokenLength--)
    {
        cout << "     " << RPNEquation[RPNPrintPos];
        cout << "\n";
        RPNPrintPos++;
    }
}

void OSProcess(string newOperator) //This function processes the Operator Stack
{
    bool PushedNewOperator = false;
    std::string newOpSTD = newOperator; //Creates an std::string version of the argument for easier comparison.
    while (PushedNewOperator == false){ //As long as the new operator is still waiting to go to the stack
        if (!OperatorStack.empty()) //If there's already an operator on the stack
        {
            if (newOpSTD == "/" || "*")
            {
                std::string OSBackSTD = OperatorStack.back(); //Create an STD version of the back of the OpStack for comparison.
                if (OSBackSTD == "+" || "-")
                {
                    OperatorStack.push_back(newOperator); //Add the tested operator to the stack
                    TokenEquation.pop_front(); //And pop it from the token equation
                    PushedNewOperator = true; //Set the flag variable to true so we stop looping
                }
                else
                {
                    RPNEquation.push_back(OperatorStack.back()); //Add the top of the operator stack to the equation
                    OperatorStack.pop_back(); //Pop this back
                }
            }
            else
            {
                RPNEquation.push_back(OperatorStack.back()); //Add the top of the operator stack to the equation
                OperatorStack.pop_back(); //Pop this back
            }
        }
        if (OperatorStack.empty())
        {
            OperatorStack.push_back(newOperator); //Add the tested operator to the stack
            TokenEquation.pop_front(); //And pop it from the token equation
            PushedNewOperator = true; //Set the flag variable to true so we stop looping
        }
    }
    //For each operator on the stack, until the following statement returns false...
    //Check if the precedence of newOperator is less than or equal to the top operator.
}

void Solver() //This function solves the RPNEquation
{
    //Push each token to the solver stack
    //If you push an operator, solve it against the stack
    //When the RPN equation is empty and the solver stack only has one token in it, you have a solution
}

1 个答案:

答案 0 :(得分:0)

一个主要问题是多行说if (newOpSTD == "/" || "*"),或者说有什么效果。这些需要更改为if (newOpSTD.compare("/") == 0 || newOpSTD.compare("*") == 0)

我认为这些检查失败意味着while循环将它们转变为while(true)