简体   繁体   English

String :: push_back()不回推

[英]String::push_back() does not push back

I am currently writing a infix to postfix converter. 我目前正在为后缀转换器编写一个中缀。 It works pretty well except I have problems adding the rest of the Stack to the postfix. 它工作得很好,除了我在将其余堆栈添加到postfix时遇到问题。 The stack is a 堆栈是一个

vector<char>

and the postfix is a String. 后缀是一个字符串。 After I'm done reading everything I try the following: 阅读完所有内容后,请尝试以下操作:

while (!stack.empty()) {
    postfix.push_back(stack.back());
    stack.pop_back();
}

But there is nothing appended. 但是没有附加任何内容。 I even tried it with Debug-mode and it seems fine, but nothing gets appended. 我什至在Debug-mode下尝试了它,看起来还不错,但是没有附加任何内容。

std::cout << postfix.c_str();

Leaves out the last operator from the stack. 从堆栈中省略最后一个运算符。 I even tried to save it temporary, but it does not get pushed. 我什至尝试将其临时保存,但没有被推送。

I can not post all four files in pastebin because of the limit. 由于限制,我无法将所有四个文件发布到pastebin中。 I use Visual Studio 2010 Ultimate and there are no errors, just no character appended. 我使用的是Visual Studio 2010 Ultimate,没有任何错误,只是没有附加任何字符。

The way you're printing out that string is needlessly complicated - and possibly wrong. 打印该字符串的方式不必要地很复杂-可能是错误的。 If you replace 如果您更换

std::cout << postfix.c_str();

with

std::cout << postfix;

You will then see what's really in the string, even if it contains embedded null characters . 然后,您将看到字符串中真正的内容, 即使它包含嵌入的null字符

Since you are using '\\0' as an error indicator but not checking for it you are likely to have embedded nulls. 由于您将'\\ 0'用作错误指示符,但未对其进行检查,因此您很可能嵌入了null。 And by using c_str() you are explicitly asking for the string to be truncated at the first null. 通过使用c_str(),您明确要求在第一个null处截断该字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM