避免从ostringstream复制字符串

时间:2014-04-30 13:37:38

标签: c++ stdstring ostringstream

我有一个名为std::string,我希望通过std::ostream接口填充数据并避免使用字符串副本。
执行此操作的一种方法是执行此操作:

bool f(std::string& out)
{
   std::ostringstream ostr;
   fillWithData(ostr);
   out = ostr.str(); // 2 copies here
   return true;
}

我需要通过out传递结果,而不能返回 ostr.str()
我想避免out = ostr.str();中的副本,因为此字符串可能非常大。

是否有某种方法,可能使用rdbuf()来将std::ostream缓冲区直接绑定到out

为了澄清,我std::stringstd::ostream的自动扩展行为感兴趣,以便调用者在调用之前不必知道大小。< / p>

更新:我刚刚意识到无关紧要的行out = ostr.str();可能需要 2 副本:

  1. str()电话
  2. 的第一个
  3. 另一个由std::string赋值运算符。

3 个答案:

答案 0 :(得分:5)

编写自己的信息流:

#include <ostream>

template <typename Char, typename Traits = std::char_traits<Char>>
class BasicStringOutputBuffer : public std::basic_streambuf<Char, Traits>
{
    // Types
    // =====

    private:
    typedef std::basic_streambuf<Char, Traits> Base;

    public:
    typedef typename Base::char_type char_type;
    typedef typename Base::int_type int_type;
    typedef typename Base::pos_type pos_type;
    typedef typename Base::off_type off_type;
    typedef typename Base::traits_type traits_type;

    typedef typename std::basic_string<char_type> string_type;

    // Element Access
    // ==============

    public:
    const string_type& str() const  { return m_str; }
    string_type& str() { return m_str; }

    // Stream Buffer Interface
    // =======================

    protected:
    virtual std::streamsize xsputn(const char_type* s, std::streamsize n);
    virtual int_type overflow(int_type);

    // Utilities
    // =========

    protected:
    int_type eof() { return traits_type::eof(); }
    bool is_eof(int_type ch) { return ch == eof(); }

    private:
    string_type m_str;
};

// Put Area
// ========

template < typename Char, typename Traits>
std::streamsize
BasicStringOutputBuffer<Char, Traits>::xsputn(const char_type* s, std::streamsize n) {
    m_str.append(s, n);
    return n;
}

template < typename Char, typename Traits>
typename BasicStringOutputBuffer<Char, Traits>::int_type
BasicStringOutputBuffer<Char, Traits>::overflow(int_type ch)
{
    if(is_eof(ch)) return eof();
    else {
        char_type c = traits_type::to_char_type(ch);
        return xsputn(&c, 1);
    }
}


// BasicStringOutputStream
//=============================================================================

template < typename Char, typename Traits = std::char_traits<Char> >
class BasicStringOutputStream : public std::basic_ostream<Char, Traits>
{
    protected:
    typedef std::basic_ostream<Char, Traits> Base;

    public:
    typedef typename Base::char_type char_type;
    typedef typename Base::int_type int_type;
    typedef typename Base::pos_type pos_type;
    typedef typename Base::off_type off_type;
    typedef typename Base::traits_type traits_type;
    typedef typename BasicStringOutputBuffer<Char, Traits>::string_type string_type;

    // Construction
    // ============

    public:
    BasicStringOutputStream()
    :   Base(&m_buf)
    {}

    // Element Access
    // ==============

    public:
    const string_type& str() const { return m_buf.str(); }
    string_type& str() { return m_buf.str(); }

    private:
    BasicStringOutputBuffer<Char, Traits> m_buf;
};

typedef BasicStringOutputStream<char> StringOutputStream;


// Test
// ====

#include <iostream>

int main() {
    StringOutputStream stream;
    stream << "The answer is " << 42;
    std::string result;
    result.swap(stream.str());
    std::cout << result << '\n';

}

注意:您可以在更复杂的实现中管理put区域指针。

答案 1 :(得分:1)

移动它:out = std::move(ostr.str())

答案 2 :(得分:0)

这是我来自https://stackoverflow.com/a/51571896/577234的自定义流缓冲区解决方案。它比Dieter的要短得多-只需要实现overflow()。通过设置缓冲区,它对于重复的ostream :: put()也具有更好的性能。使用ostream :: write()进行大写操作的性能相同,因为它调用xsputn()而不是overflow()。

class MemoryOutputStreamBuffer : public streambuf
{
public:
    MemoryOutputStreamBuffer(vector<uint8_t> &b) : buffer(b)
    {
    }
    int_type overflow(int_type c)
    {
        size_t size = this->size();   // can be > oldCapacity due to seeking past end
        size_t oldCapacity = buffer.size();

        size_t newCapacity = max(oldCapacity + 100, size * 2);
        buffer.resize(newCapacity);

        char *b = (char *)&buffer[0];
        setp(b, &b[newCapacity]);
        pbump(size);
        if (c != EOF)
        {
            buffer[size] = c;
            pbump(1);
        }
        return c;
    }
  #ifdef ALLOW_MEM_OUT_STREAM_RANDOM_ACCESS
    streampos MemoryOutputStreamBuffer::seekpos(streampos pos,
                                                ios_base::openmode which)
    {
        setp(pbase(), epptr());
        pbump(pos);
        // GCC's streambuf doesn't allow put pointer to go out of bounds or else xsputn() will have integer overflow
        // Microsoft's does allow out of bounds, so manually calling overflow() isn't needed
        if (pptr() > epptr())
            overflow(EOF);
        return pos;
    }
    // redundant, but necessary for tellp() to work
    // https://stackoverflow.com/questions/29132458/why-does-the-standard-have-both-seekpos-and-seekoff
    streampos MemoryOutputStreamBuffer::seekoff(streamoff offset,
                                                ios_base::seekdir way,
                                                ios_base::openmode which)
    {
        streampos pos;
        switch (way)
        {
        case ios_base::beg:
            pos = offset;
            break;
        case ios_base::cur:
            pos = (pptr() - pbase()) + offset;
            break;
        case ios_base::end:
            pos = (epptr() - pbase()) + offset;
            break;
        }
        return seekpos(pos, which);
    }
#endif    
    size_t size()
    {
        return pptr() - pbase();
    }
private:
    std::vector<uint8_t> &buffer;
};

他们说好的程序员是个懒惰的程序员,所以这是我想到的另一种实现,它需要更少的自定义代码。但是,存在内存泄漏的风险,因为它会劫持MyStringBuffer内的缓冲区,但不会释放MyStringBuffer。实际上,它对于GCC的streambuf不会泄漏,我使用AddressSanitizer确认了这一点。

class MyStringBuffer : public stringbuf
{
public:
  uint8_t &operator[](size_t index)
  {
    uint8_t *b = (uint8_t *)pbase();
    return b[index];
  }
  size_t size()
  {
    return pptr() - pbase();
  }
};

// caller is responsible for freeing out
void Test(uint8_t *&_out, size_t &size)
{
  uint8_t dummy[sizeof(MyStringBuffer)];
  new (dummy) MyStringBuffer;  // construct MyStringBuffer using existing memory

  MyStringBuffer &buf = *(MyStringBuffer *)dummy;
  ostream out(&buf);

  out << "hello world";
  _out = &buf[0];
  size = buf.size();
}