繁体   English   中英

为什么我的std :: string被切断了?

[英]why is my std::string being cut off?

我初始化一个字符串,如下所示:

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)";

并且myString最终像这样被切断:

“快速的棕色狐狸跳过了懒狗”是英语的pangram(包含

我在哪里可以设置尺寸限制? 我尝试了以下操作但未成功:

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)";

非常感谢!

当然,只是调试器将其切断(xcode)。 我刚刚开始使用xcode / c ++,因此非常感谢您的快速回复。

请尝试以下操作(在调试模式下):

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);

(第二个)断言是否失败?

你确定吗?

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)

没有充分的理由说明应该发生这种情况!

在打印或显示文本时,输出设备会缓冲输出。 您可以通过输出'\\ n'或使用std::endl或执行flush()方法来告诉它刷新缓冲区(显示所有剩余文本):

#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;
}

有关缓冲区的更多信息,请在堆栈溢出中搜索“ C ++输出缓冲区”。

暂无
暂无

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

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