8

我正在尝试使用方法 pubsetbuf 修改字符串流对象的字符串缓冲区而不必复制字符串,但它不起作用。我正在关注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

4

1 回答 1

11

不应该设置内容。pubsetbuf来电virtual setbuf

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

15 效果:实现定义,除了 setbuf(0,0) 没有效果。

16 回报:这个。

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

virtual _Myt *__CLR_OR_THIS_CALL setbuf(_Elem *, streamsize)
    {   // offer buffer to external agent (do nothing)
    return (this);
    }
于 2012-09-18T16:55:19.480 回答