【问题标题】:stringstream->rdbuf()->pubsetbuf is not setting the bufferstringstream->rdbuf()->pubsetbuf 没有设置缓冲区
【发布时间】:2012-09-10 23:31:09
【问题描述】:

我正在尝试使用 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

【问题讨论】:

标签: c++ stl stringstream stringbuffer


【解决方案1】:

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

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

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

16 返回: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++ 编译器中都能正常工作。
猜你喜欢
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多