c++ - stringstream->rdbuf()->pubsetbuf 没有设置缓冲区

标签 c++ stl stringstream stringbuffer

我正在尝试使用 pubsetbuf 方法修改 stringstream 对象的 stringbuffer 而不必复制字符串,但它不起作用。我正在关注 http://www.cplusplus.com/reference/iostream/streambuf/pubsetbuf/ 中的文档.这是我的示例代码:

#include <iostream>
#include <sstream>

int main(int argc, char* argv[])
{
    std::stringstream stream("You say goodbye");
    char replace[] = {"And I say hello"};
    std::cout << stream.str() << std::endl; // Checking original contents
    stream.rdbuf()->pubsetbuf(replace, 16); // Should set contents here
    std::cout << stream.str() << std::endl; // But don't :(
    return 0;
}

输出是:

You say goodbye
You say goodbye

我知道我可以使用 stream.str(replace),但是这个方法复制了“replace”的值,我不想复制。

我错过了什么?

更新:我正在使用 VS2010

最佳答案

不应该设置内容。 pubsetbuf 调用虚拟 setbuf

basic_streambuf<charT,traits>* setbuf(charT* s, streamsize n);

15 Effects: implementation-defined, except that setbuf(0,0) has no effect.

16 Returns: this.

VS 2010. basic_stringbuf 中没有重载虚方法setbuf,它使用basic_streambuf中的默认值

virtual _Myt *__CLR_OR_THIS_CALL setbuf(_Elem *, streamsize)
    {   // offer buffer to external agent (do nothing)
    return (this);
    }

关于c++ - stringstream->rdbuf()->pubsetbuf 没有设置缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12481463/

相关文章:

c++ - 为什么我们可以在 `std::move` 对象上使用 `const`?

c++ - 简要说明引用折叠规则请求 : (1) A& & -> A& , (2) A& && -> A& , (3) A&& & -> A& , 和 (4) A&& && -> A&&

c++ - 您可以使用自定义比较器将 std::map 转换为无序映射吗?

c++ - 如何将 lambda 用于 std::find_if

xml - 使用标准模块 `parsexml` ,如何通过纯字符串流或字符串而不是文件解析 XML

c++ - 从函数返回迭代器

c++ - main.cpp|45|错误: expected primary-expression before "int" and "double"

c++ - 存储霍夫曼树的有效方法

使用类、getline 和 stringstream 进行解析的 C++ 练习

c++ - stringstream clear 和 str 有什么区别