c++ - 堆栈检查 C++ 中的 html 标签平衡

标签 c++

我在尝试使用堆栈检查 html 文件是否具有平衡标签时遇到问题。我有使用 C++ 文件的程序。它检查括号是否平衡。问题是标签太适合字符了。我在字符常量中遇到错误太多字符。有办法解决他的问题吗?

bool balanced(char A[], int n) {
int i;

stack <char> a;

    for (i = 0; i < n; i++) {

    if (A[i] == '<head>' || A[i] == '<title>' || A[i] == '<body>') {
        a.push(A[i]);
    }
    else
    {

        switch (A[i]) {

        case '</head>':

            a.top();
            a.pop();

            break;
        case '</title>':

            a.top();
            a.pop();

            break;
        case '</body>':

            a.top();
            a.pop();

            break;



        }


    }



}

最佳答案

你不能这样做

A[i] == '<head>'

A[i] 是一个字符而不是字符串。你应该使用 std::string。如果 'A' 是一个字符串,你可以这样做

A =="<head>"

或使用 strcmp(不是惯用的 C++)

关于c++ - 堆栈检查 C++ 中的 html 标签平衡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49701505/

相关文章:

c++ - 元编程 : using a const array

c++ - 在调试中删除标准 C++ 库的 Visual Studio 全局互斥锁

c++ - 从 pcap 文件中过滤数据包

c++ - std::vector range constructor [start, end) 是复制还是仅引用数据?

c++ - std::error_code,my_error::check_block == my_error::validate && my_error::accept_block == my_error::validate

c++ - 编译 Qt hello World 时对 vtable 的 undefined reference

c++ - OpenGL 编码(特别是 w.r.t. 面向对象)有哪些最佳实践?

c++ - C 风格的字符串

c++ - 如何在 Xcode 中使用 c++ std::vector 元素类型和值查看?

c++ - 谷歌测试/模拟 Qt 信号