c++ - 表达式:双端队列迭代器不可取消引用

标签 c++ stl expression postfix-notation infix-notation

我正在用 C++ 编写程序以将中缀转换为后缀。这是我的代码。

#include<iostream>
#include<stack>
#include<string.h>

using namespace std;


int getPrecedence( char tmp )
{
    if(tmp=='+')
    {
        return 1;
    }
    else if(tmp == '-')
    {
        return 1;
    }
    else if(tmp == '*')
    {
        return 2;
    }
    else if(tmp == '/')
    {
        return 2;
    }
}

int main()
{

    stack<char> st;

    char expression[10];


    //cout<<"Enter expression : ";
    //cin>>expression;

    strcpy(expression,"a+b*c/d-e");

    char postfix[100];  // its postfix string
    int counter=0;

    int i=0;


    while( expression[i] != '\0' )  // iterate till '/0' does not come.
    {
        if(expression[i]== '+' || expression[i]== '-' || expression[i]== '*' || expression[i]== '/'  )
        {
            if( st.empty() )
            {
                st.push(expression[i]);
            }
            else // when stack not empty
            {
                int topPrecedence = getPrecedence( st.top() );
                int expressionPrecedence = getPrecedence( expression[i] );


                while( !(topPrecedence < expressionPrecedence) )
                {
                    postfix[counter++] = st.top();
                    st.pop();
                    topPrecedence = getPrecedence( st.top() );
                }

                if( st.empty() )
                {
                    st.push( expression[i] );
                }

                if( topPrecedence < expressionPrecedence )
                {
                    st.push( expression[i] );
                }


            }
        }
        else // when its an alphabet 
        {
            postfix[counter++] = expression[i];
        }


        i++;
    } // outer while ends 

    while( ! st.empty() )
    {
        postfix[counter++] = st.top();
        st.pop();
    }


    postfix[counter] = '\0';
    i=0;

    while( postfix[i] != '\0' )
    {
        cout<<postfix[i]<<" ";
        i++;
    }



    system("pause");
    return 0;
}

例如,如果输入表达式是a+b*c/d-e。直到 d 它转换表达式后缀。但是当 - 来的时候。它显示以下错误。

表达式:双端队列迭代器不可取消引用

显然它与 Queue 有关,我什至没有使用过 Queue。

屏幕截图: https://www.facebook.com/photo.php?fbid=241461649373492&set=a.118955391624119.1073741827.100005289761090&type=1

最佳答案

堆栈是一个容器适配器。

默认情况下它采用deque。您可能只是从一个空堆栈中弹出。

关于c++ - 表达式:双端队列迭代器不可取消引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22648290/

相关文章:

c++ - 如何构建加密库

c++ - 'return' 之前的预期主表达式

c++ - 从上到下横向打印 BST

C# MVC 模型表达式到值

regex - Notepad++如何删除字符串后的所有字符

c# - 如何为 Like 创建 System.Linq.Expressions.Expression?

c++ - 是否可以存储从 C++ 中的文本文件读取的换行符?

c++ - 找出父类型 vector 元素的子类型

c++ - 如何扩展 STL 类以拥有自己的方法?

c++ - remove_if 后释放内存