stringstream-> rdbuf() - > pubsetbuf不设置缓冲区 [英] stringstream->rdbuf()->pubsetbuf is not setting the buffer

查看:634
本文介绍了stringstream-> rdbuf() - > pubsetbuf不设置缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图修改stringstream对象的stringbuffer,而不必复制一个字符串,使用方法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 [] = {我说hello};
std :: cout<< stream.str()<< std :: endl; //检查原始内容
stream.rdbuf() - > pubsetbuf(replace,16); //应该在这里设置内容
std :: cout<< stream.str()<< std :: endl; //但不要:(
return 0;
}

输出是:

 你说再见
你说goodbye

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



我缺少什么?



更新:我使用VS2010

解决方案

不应设置内容 pubsetbuf 调用 virtual setbuf

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




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



16返回:



basic_stringbuf 中的虚方法 setbuf 没有重载,它使用默认从 basic_streambuf

  virtual _Myt * __ CLR_OR_THIS_CALL setbuf(_Elem *,streamsize)
{//向外部代理提供缓冲(do nothing)
return(this);
}


I am trying to modify a stringbuffer of a stringstream object without having to copy a string, using the method pubsetbuf, but it is not working. I am following the documentation in http://www.cplusplus.com/reference/iostream/streambuf/pubsetbuf/. Here is my example code:

#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;
}

And the output is:

You say goodbye
You say goodbye

I know I can use stream.str(replace), but this method copies the value of 'replace', and I don't want to make a copy.

What am I missing?

Update: I'm using VS2010

解决方案

Not should set contents. pubsetbuf calls virtual 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. There is no overload of virtual method setbuf in basic_stringbuf, it uses default from basic_streambuf

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

这篇关于stringstream-&gt; rdbuf() - &gt; pubsetbuf不设置缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆