采用如下代码实现检查输入的字符串括号是否匹配,横线上应填入的代码为( )。

jd344958 单选题 共 个子题
#include <iostream>
#include <stack>
#include <string>

using namespace std;

bool is_valid(string s) {
    stack<char> st;
    char top;

    for (char& ch : s) {
        if (ch == '(' || ch == '{' || ch == '[') {
            st.push(ch); // 左括号入栈
        }
        else
        {
            if (st.empty())
                return false;
            ———————————————————————— // 在此处填入代码
            if ((ch == ')' && top != '(') ||
                (ch == '}' && top != '{') ||
                (ch == ']' && top != '[')) {
                return false;
            }
        }
    }

    return st.empty(); // 栈为空则说明所有括号匹配成功
}
A

top = st.top(); st.pop();

B

st.pop(); top = st.top();

C

st.pop(); top = st.front();

D

top = st.front(); st.pop();

正在加载题目数据…
已答 /
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析
讨论
发布
登录后参与讨论 立即登录
发布讨论需先绑定并验证手机号 去绑定手机号
加载中…
我
我
回复 @
回复 @
发送
还没有人讨论,来发表第一条吧