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++ 如何使用std::ends覆盖std::ostringstream中的字符串?_C++_String_Overwrite_Ostringstream - Fatal编程技术网

C++ 如何使用std::ends覆盖std::ostringstream中的字符串?

C++ 如何使用std::ends覆盖std::ostringstream中的字符串?,c++,string,overwrite,ostringstream,C++,String,Overwrite,Ostringstream,我想覆盖std::ostringstream中的字符串,但std::ends不起作用。这是VS2012的问题,还是我做错了什么?下面是示例代码: std::ostringstream foo; foo << "1,2,3,4,5,6"; std::cout << foo.str(); // prints: 1,2,3,4,5,6 foo.clear(); foo.seekp( std::ios_base::beg ); foo << "A,B,C" <

我想覆盖
std::ostringstream
中的字符串,但
std::ends
不起作用。这是VS2012的问题,还是我做错了什么?下面是示例代码:

std::ostringstream foo;

foo << "1,2,3,4,5,6";
std::cout << foo.str(); // prints: 1,2,3,4,5,6
foo.clear();
foo.seekp( std::ios_base::beg );
foo << "A,B,C" << std::ends;
std::cout << foo.str(); // prints: A,B,C,4,5,6 NOT just: A,B,C
std::ostringstream foo;
foo实际上它是打印的

注意
[NUL]
,它对应于
std::ends
,并且在
3

函数的
ostringstream::str()
返回一个跟踪其长度并忽略任何可能的行尾字符的容器

因此,要获得一种尊重字符串结尾字符的格式,您可以使用
char*
(或者
wchar\u t*
(如果使用unicode支持编译)表示法,这种表示法是通过
std::string::c_str()
获得的,如
foo.str().c_str()

实际打印

注意
[NUL]
,它对应于
std::ends
,并且在
3

函数的
ostringstream::str()
返回一个跟踪其长度并忽略任何可能的行尾字符的容器


因此,要获得一种尊重字符串结尾字符的格式,您可以使用
char*
(或者
wchar\u t*
(如果使用unicode支持编译)表示法,这种表示法是通过
std::string::c_str()
获得的,如
foo.str().c_str()

为什么要使用
std::ends
?它是
std::ostream
时代遗留下来的东西,它确保生成的
char[]
以null结尾。您通常不希望在
std::ostringstream
上使用它


在您展示的代码中,我觉得您真正想要的是两个独立的
std::ostringstream
(尽管可以通过调用
foo.str(“”;
)来清除数据)。

为什么要使用
std::ends
?它是
std::ostream
时代遗留下来的东西,它确保生成的
char[]
以null结尾。您通常不希望在
std::ostringstream
上使用它


在您展示的代码中,我认为您真正想要的是两个独立的
std::ostringstream
(尽管可以通过调用
foo.str(“”;
)来清除数据)。

如果您只想覆盖字符串内容,只需使用
str(new_string)
方法,如下所示:

std::ostringstream foo;

foo << "1,2,3,4,5,6";
std::cout << foo.str(); // prints: 1,2,3,4,5,6
// clear just reset error state flags
foo.clear();
// Replace the content of the stream
foo.str("A,B,C");
std::cout << foo.str(); // Should print: A,B,C
std::ostringstream foo;

foo如果您只想覆盖字符串内容,只需使用
str(new\u string)
方法,如下所示:

std::ostringstream foo;

foo << "1,2,3,4,5,6";
std::cout << foo.str(); // prints: 1,2,3,4,5,6
// clear just reset error state flags
foo.clear();
// Replace the content of the stream
foo.str("A,B,C");
std::cout << foo.str(); // Should print: A,B,C
std::ostringstream foo;
第三条语句中的foofoo.clear()不会删除foo中的数据。
在第4条语句中,您使用了文件位置来指向起始点。
所以下一个语句从开头取值,所以值1,2,3被A,B,C覆盖

使用foo.str(“”)代替foo.clear()

在第三条语句中,foo.clear()不会删除foo中的数据。 在第4条语句中,您使用了文件位置来指向起始点。 所以下一个语句从开头取值,所以值1,2,3被A,B,C覆盖


使用foo.str(“”)代替foo.clear()

除非
std::ios_base::beg
0
foo.seekp(std::ios_base::beg)
是错误的。您应该执行
foo。请改用seekp(0)
。@0x499602D2是的,
std::ios_base::beg
枚举的0元素。在这里使用0可能是一种滥用。除非
std::ios_base::beg
0
foo。seekp(std::ios_base::beg)
是错误的。您应该执行
foo。请改用seekp(0)
。@0x499602D2是的,
std::ios_base::beg
枚举的0元素。在这里使用0可能是一种滥用,因此解决方案可能是将其转储到
const char*
?比如
foo.str().c_str()
?@JonathanMee:如果你想让事情变得更复杂,这是一个解决方案。在重用流之前清空它会更简单,而不是胡乱使用奇怪的C风格的习惯用法。@MikeSeymour如果可能的话,我宁愿避免重新分配内存,因为我在代码中大量重用这个流。尽管使用
常量字符*
也是浪费。我最初的想法来自于这个问题:@JonathanMee:如果它需要在清除后重新分配内存,我会感到惊讶。它只需将其内部指针重置为其缓冲区的开始。(但你应该检查一下,在不太可能的情况下,这可能是一个性能瓶颈)。@MikeSeymour因为
std::ostringstream
处于一个大循环中,是的,内存正在通过
foo.str(std::string())重新分配。我只希望有一种方法不要屈尊于
foo.str().c_str()
。那么解决方案可能是将其转储到
常量char*
?比如
foo.str().c_str()
?@JonathanMee:如果你想让事情变得更复杂,这是一个解决方案。在重用流之前清空它会更简单,而不是胡乱使用奇怪的C风格的习惯用法。@MikeSeymour如果可能的话,我宁愿避免重新分配内存,因为我在代码中大量重用这个流。尽管使用
常量字符*
也是浪费。我最初的想法来自于这个问题:@JonathanMee:如果它需要在清除后重新分配内存,我会感到惊讶。它只需将其内部指针重置为其缓冲区的开始。(但你应该检查一下,在不太可能的情况下,这可能是一个性能瓶颈)。@MikeSeymour因为
std::ostringstream
处于一个大循环中,是的,内存正在通过
foo.str(std::string())重新分配。我只希望有一种方法不要屈尊于
foo.str().c_str()