C++ 为什么我的std::字符串被切断?
我按如下方式初始化字符串:C++ 为什么我的std::字符串被切断?,c++,string,resize,standard-library,C++,String,Resize,Standard Library,我按如下方式初始化字符串: 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 = "'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)";
非常感谢!请尝试以下操作(在调试模式下):
(第二次)断言是否失败?尝试以下操作(在调试模式下):
(第二次)断言失败了吗?您确定吗
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)
没有充分的理由说明为什么会发生这种情况!你确定吗
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;
}
#包括
使用std::cout;
使用std::endl;
内部主(空)
{
std::string myString=
“‘敏捷的棕色狐狸跳过懒狗’”//
“是一种英语pangram(一个短语)/这些连续的文本
自动包含“/”文本的所有字母的。
“字母表”;
//方法1:使用“\n”
//换行符强制缓冲区刷新。
cout当打印或显示文本时,输出机器缓冲输出。您可以通过输出“\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;
}
#包括
使用std::cout;
使用std::endl;
内部主(空)
{
std::string myString=
“‘敏捷的棕色狐狸跳过懒狗’”//
“是一种英语pangram(一个短语)/这些连续的文本
自动包含“/”文本的所有字母的。
“字母表”;
//方法1:使用“\n”
//换行符强制缓冲区刷新。
当然,这只是调试器将其关闭(xcode)。我刚刚开始使用xcode/c++,非常感谢您的快速回复。当然,这只是调试器将其关闭(xcode)。我刚刚开始使用xcode/c++,非常感谢您的快速回复。字符串没有大小限制(至少,不是一个小到100,可能有一个以兆字节为单位的实现限制)。错误在代码中的其他地方。编写一个简短的程序来演示问题,然后发布。您的终端是否有100个字符宽?截断可能发生在输出上(尽管我不知道是什么终端截断而不是换行)。字符串不应该被截断。您是如何检查/显示它的?您如何知道它被截断了?您是打印出来的,还是查看了调试器(哪一个?)?哎呀,当然是调试器截断了它(xcode).我刚刚开始使用xcode/c++,非常感谢您的快速回复。Steve,请在回答中写下您的评论,这样我就可以接受了,“您的终端有100个字符宽吗?”让我说“哦”.没关系,你可以只回答你自己的问题,或者编辑问题,说问题已经解决。我的评论是评论,因为这都是问题和“可能”,我很高兴能帮上忙,但我并不声称自己已经回答了这个问题。顺便说一句,优秀的用户名直到我输入时才识别出来。字符串没有大小限制(至少,没有一个小到100,可能有以兆字节为单位的实现限制)。错误在代码的其他地方。编写一个简短的程序来演示问题,然后发布。您的终端是否有100个字符宽?截断可能发生在输出上(尽管我不知道哪些终端截断而不是换行)。字符串不应该被截断。您是如何检查/显示它的?您如何知道它被截断了?您是打印出来的,还是查看了调试器(哪一个?)?哎呀,当然是调试器截断了它(xcode).我刚刚开始使用xcode/c++,非常感谢您的快速回复。Steve,请在回答中写下您的评论,这样我就可以接受了,“您的终端有100个字符宽吗?”让我说“哦”.没关系,你可以只回答你自己的问题,或者编辑问题,说问题已经解决。我的评论是评论,因为这都是问题和“可能”,我很高兴能帮上忙,但我并不声称自己已经回答了这个问题。顺便说一句,优秀的用户名直到我键入时才识别出来。您可以在调试中尝试,否则您的编译器将忽略断言调用。@ereOn是的。我添加了一个额外的测试来测试我们是否处于调试模式。assert()之后
抱怨错误的表达式,它调用了abort()
。在我的环境中没有。我可以在断言失败后继续。您可以在调试中尝试,或者编译器将忽略断言调用。@ereOn Yes。我添加了一个额外的测试来测试我们是否处于调试模式。在assert()之后
抱怨错误的表达式,它调用了abort()
。在我的环境中没有。我可以在断言失败后继续。