【问题标题】:why is my std::string being cut off?为什么我的 std::string 被切断了?
【发布时间】:2011-04-12 15:57:32
【问题描述】:

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

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

非常感谢!

【问题讨论】:

  • 字符串没有大小限制(至少,不会像 100 一样小,可能存在以兆字节为单位的实现限制)。该错误在您的代码中的其他地方。编写一个演示问题的简短程序,然后发布。你的终端是 100 个字符宽吗?截断可能发生在输出上(尽管我不知道哪些终端会截断而不是换行)。
  • 字符串不应该被切断。你是如何检查/展示它的?
  • 你怎么知道它被切断了?你是打印出来的,还是查看调试器(哪个?)?
  • 哎呀,当然只是调试器将其切断(xcode)。我刚刚开始使用 xcode/c++,非常感谢您的快速回复。史蒂夫,请在答案中写下您的评论,以便我接受它,“您的终端是 100 个字符宽吗?”让我“哎呀”。
  • 没关系,您可以只回答自己的问题,或者编辑问题以说明它已解决。我的评论是评论,因为这都是问题和“也许”,我很高兴能提供帮助,但我并没有声称实际上已经回答了这个问题。顺便说一句,优秀的用户名直到我输入它才认出来。

标签: c++ string resize standard-library


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你确定吗?

    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)
    

    没有充分的理由为什么会发生这种情况!

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • 您可以在调试中尝试,否则您的编译器将忽略断言调用。
      • @ereOn 是的。我添加了一个额外的测试来测试我们是否处于调试模式。
      • assert() 抱怨表达式错误后,它调用abort()
      • 不在我的环境中。断言失败后我可以继续。
      【解决方案4】:

      在打印或显示文本时,输出机器会缓冲输出。您可以通过输出 '\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;
      }
      

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

      【讨论】:

        猜你喜欢
        • 2021-04-18
        • 2020-05-21
        • 1970-01-01
        • 2020-05-13
        • 2015-06-25
        • 1970-01-01
        • 1970-01-01
        • 2012-04-17
        • 1970-01-01
        相关资源
        最近更新 更多