Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ostringstream和ends_C++_String_Stream - Fatal编程技术网

C++ ostringstream和ends

C++ ostringstream和ends,c++,string,stream,C++,String,Stream,我一直在使用其他人的代码,并注意到在OstringTeam的所有使用中,他们都习惯于显式地附加std::ends 这是我从未做过的事情,也从未遇到过问题 它似乎没有,但是std::ends是否应该对以下代码产生任何影响 ostringstream message; message << "Hello world, version " << 2 /* << std::ends ??? */; printf( "%s\n", message.str().c_str

我一直在使用其他人的代码,并注意到在OstringTeam的所有使用中,他们都习惯于显式地附加
std::ends

这是我从未做过的事情,也从未遇到过问题

它似乎没有,但是
std::ends
是否应该对以下代码产生任何影响

ostringstream message;
message << "Hello world, version " << 2 /* << std::ends ??? */;
printf( "%s\n", message.str().c_str() );
ostringstream消息;

消息追加
std::ends
在这里毫无意义,因为
stringstream
c_str
返回以null结尾的
char*
。对于(现在已弃用的)strstream
s,情况并非如此,其中需要附加
std::ends
。我相信作者根本不知道这种改变的行为。

它不应该,
c_str()
返回以NUL结尾的字符串。

+1回答得好。对于stringstream,有些情况下,
结束
是有用的:尽管上面的链接中结束是有用的,但是如果在没有预期的情况下添加“额外”字符,它们可能是危险的。我的同事想知道为什么他的stringstream构造的字符串与数据库中的字符串不完全相同,这是因为它有一个嵌入的\0字符。他是否有可能键入了
std::endl
?@litb-不,我认为目的肯定是结束,而且多次出现您的printf错误,它缺少一个格式字符串。@Brian Neal很好。就像我不再在正确的代码中使用printf一样。。。