开发者

why is my std::string being cut off?

I initialize a string as follows:

std::string myString = "'The quick brown fox jumps over the lazy dog' is an English-language pangram (a phrase that contains all of the letters of the alphabet)";

and the myString ends up being cut off like this:

'The quick brown fox jumps over the lazy dog' is an English-language pangram (a phrase that contains

Wh开发者_如何学Pythonere can i set the size limit? I tried the following without success:

std::string myString;
myString.resize(300);
myString = "'The quick brown fox jumps over the lazy dog' is an English-language pangram (a phrase that contains all of the letters of the alphabet)";

Many thanks!


Of course it was just the debugger cutting it off (xcode). I'm just getting started with xcode/c++, so thanks a lot for the quick replies.


Are you sure?

kkekan> ./a.out 
'The quick brown fox jumps over the lazy dog' is an English-language pangram (a phrase that contains all of the letters of the alphabet)

There is no good reason why this should have happen!


Try the following (in debug mode):

assert(!"Congratulations, I am in debug mode! Let's do a test now...")
std::string myString = "'The quick brown fox jumps over the lazy dog' is an English-language pangram (a phrase that contains all of the letters of the alphabet)";
assert(myString.size() > 120);

Does the (second) assertion fail?


When printing, or displaying text, the output machinery buffers the output. You can tell it to flush the buffers (display all remaining text) by output a '\n' or using std::endl or executing the flush() method:

#include <iostream>
using std::cout;
using std::endl;

int main(void)
{
  std::string myString =
    "'The quick brown fox jumps over the lazy dog'" // Compiler concatenates
    " is an English-language pangram (a phrase"     // these contiguous text
    " that contains all of the letters of the"      // literals automatically.
    " alphabet)";
  // Method 1:  use '\n'
  // A newline forces the buffers to flush.
  cout << myString << '\n';

  // Method 2:  use std::endl;
  // The std::endl flushes the buffer then sends '\n' to the output.
  cout << myString << endl;

  // Method 3:  use flush() method
  cout << myString;
  cout.flush();

  return 0;
}

For more information about buffers, search Stack Overflow for "C++ output buffer".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜