0

Assuming:

std::string ToShow,NumStr;

The following displays "This is 19 ch00":

ToShow = "This is nineteen ch";
ToShow.resize(ToShow.length()+0);
NumStr = "00";
ToShow += NumStr;
mvaddstr(15,0,ToShow.c_str());

And the following displays "This is 19 ch ":

ToShow = "This is nineteen ch";
ToShow.resize(ToShow.length()+1);
NumStr = "0";
ToShow += NumStr;
mvaddstr(16,0,ToShow.c_str());

In the second case, operator+= isn't adding the string "0" to the end of ToShow. Does anyone know why?

4

1 に答える 1

6

私の推測は次のとおりです。

サイズ変更する値を指定しないためToShow.Resize(ToShow.length()+1)、文字列は次のようになります。

"This is nineteen ch\0"

そして後+= NumStr

"This is nineteen ch\00"

これは、c_str を呼び出した後、最初の部分にトリミングされ、\0次のようになります。

"This is nineteen ch"

(C 文字列は null で終了しますが、std::strings はそうではありません)

.resize(someLength, ' ')代わりに電話してみてください。

于 2012-06-28T18:55:13.560 に答える