简体   繁体   中英

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. 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.

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. I use Visual Studio 2010 Ultimate and there are no errors, just no character appended.

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 .

Since you are using '\\0' as an error indicator but not checking for it you are likely to have embedded nulls. And by using c_str() you are explicitly asking for the string to be truncated at the first null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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