C++ 我应该使用地图、3D阵列还是使用堆栈?

C++ 我应该使用地图、3D阵列还是使用堆栈?,c++,arrays,dictionary,stack,C++,Arrays,Dictionary,Stack,到目前为止,我可以让它在任何情况下都能很好地工作,除了开放大括号比封闭大括号多的情况。每当堆栈仍然包含任何打开的大括号时,我都不知道如何找到该打开的大括号。我想知道是否包括一个映射或更改我的堆栈以获取3D(?)数组值,以便它可以包含字符的坐标?如果有一个更简单的方法,如果你们能帮我找到它,那就太好了。请不要只告诉我答案,因为我被困在如何让它工作的问题上 非常感谢你 void Brackets::check_bracket() { for (int

到目前为止,我可以让它在任何情况下都能很好地工作,除了开放大括号比封闭大括号多的情况。每当堆栈仍然包含任何打开的大括号时,我都不知道如何找到该打开的大括号。我想知道是否包括一个映射或更改我的堆栈以获取3D(?)数组值,以便它可以包含字符的坐标?如果有一个更简单的方法,如果你们能帮我找到它,那就太好了。请不要只告诉我答案,因为我被困在如何让它工作的问题上

非常感谢你

    void Brackets::check_bracket()
        {
            for (int a = 0; a < line_num; a++)//loops through every line
            {
                for (int c = 0; c < lines[a].length(); c++)//loops through every char in string
                {
                    while (lines[a].find("//") != -1 || lines[a].find("cout") != -1) a++;//checks if there is a comment
                    if (lines[a][c] == '(' || lines[a][c] == '[' || lines[a][c] == '{')//checks if the brace is an open brace
                    {
                        //DBG::cout << "yes" << lines[a][c] << endl;
                        bracket.push(lines[a][c]);//if yes then it pushs the brace in
                    }
                    if (lines[a][c] == ')' || lines[a][c] == ']' || lines[a][c] == '}')//checks if the brace is a close brace
                    {
                        //DBG::cout << bracket.empty() << lines[a][c] << endl;
                        if (bracket.empty())
                        {//if empty then tells user the bracket does not match any open parenthesis
                            //DBG::cout << 1;
                            cout << "closed parenthesis " << lines[a][c] << " does not match any open parenthesis.";
                            return;
                        }
                        //else if the brackets do match an open brace then it pops it out
                        else if ((lines[a][c] == ')' && bracket.top() == '(') || (lines[a][c] == ']' && bracket.top() == '[') || (lines[a][c] == '}' && bracket.top() == '{'))
                        {
                            //DBG::cout << 2;
                            bracket.pop();
                        }//checks if bracket matches any open parenthesis
                        else
                        {
                            //DBG::cout << 3 << a << endl;
                            cout << "Line " << a+1 << ":" << " error at column " << c+1 << ":" << endl;
                            cout << lines[a] << endl;
                            for (int o = 0; o < c; o++)
                                cout << " ";
                            cout << "^" << endl;
                            cout << "closed parenthesis " << lines[a][c] << " does not match open parenthesis " << bracket.top() << " from row " << find_last(a, bracket.top());
                            return;
                        }
                    }
                }
            }
            if (!bracket.empty())//if bracket still contains something in the stack then it finds where the leftover open brace is
            {
                cout << "Unmatched open parenthesis " << bracket.top() << " at row "; //im stuck
            }
            else
            cout << "No parenthesis errors."<<endl;//otherwise no parenthesis errors
        }
        //finds the last problem brace
        string Brackets::find_last(int line, char chk)
        {
            string row_col = "";
            for (int a = line; a >= 0; a--)//loops through lines backwards
            {
                for (int b = lines[a].length() - 1; b >= 0; b--)//loops through length backwards
                {
                    if (lines[a][b] == chk)//checks if the char equals the problem brace
                    {
                        row_col = to_string(a+1) + " and column " + to_string(b+1)+"\n";//returns string of location
                        return row_col;
                    }
                }
            }
            return "";
        }

private:
    string lines[1000];//contains code lines
    int line_num = 0;//number of lines
    stack<char> bracket;//brackets
void方括号::检查方括号()
{
for(int a=0;a//我只想在堆栈上存储一个简单的结构

struct Bracket {
  char type;
  int line;
  int col;
}

顺便说一句:存储预期的结束括号(而不是开始括号)可能是有意义的:计算推送的位置很简单,并且可以简化当前由3x
&
和2x
|

组成的匹配。您正在寻找匹配的括号吗

坚持使用堆栈,但存储一个结构,而不是像Stefan建议的那样只存储字符。因为括号本质上与堆栈类似,即括号越大,堆栈越深,而括号越小,堆栈就会弹出。因此,理想情况下,在一切正常的情况下,您应该有一个空的堆栈。使用struct instea的原因是仅字符的d是,因此可以保留有关括号的更多信息

3D数组将使用太多的连续空间来执行您想要执行的操作。如果您解析一个大文件或多个文件,则会变得非常糟糕

Map没有那么糟糕,但并不理想。您只能有一个键和值,这可能无法提供您所需的所有信息。管理结构的附加功能也会带来不必要的负担,只需简单地推送和弹出堆栈即可解决

因此,总而言之: 1) 创建一个包含字符、行和列的结构(Stefan Haustein建议) 2) 找到支架后,按下包含相关信息的结构 3) 如果达到EOF且堆栈不为空,则弹出堆栈的其余部分并打印结构的信息
如果您到达堆栈的末尾,但仍然有一个紧括号,请打印信息

stackoverflow上的所有问题都必须包含一个作为问题本身的一部分,而不是一个外部链接。请相应地编辑您的问题。问题中包含的代码必须最少,并且必须完整。@Balsdkjaksdjfs-只需添加另一个堆栈即可h行/行号和push/pop在该堆栈上同时进行。我将尝试再添加两个堆栈。谢谢,它现在可以工作了!:d谢谢我使用了你的方法来简化我的函数!