如何获得字符串流的字节长度.
stringstream.str().length();
将内容复制到std :: string中.我不想复制.
或者,如果任何人都可以建议另一个在内存中工作的iostream,可以通过写入另一个ostream来传递,并且可以轻松地获得它的大小我将使用它.
提供字符串流长度的解决方案,包括构造函数中提供的任何初始字符串:
#include <sstream>
using namespace std;
#ifndef STRINGBUFFER_H_
#define STRINGBUFFER_H_
class StringBuffer: public stringstream
{
public:
    /**
     * Create an empty stringstream
     */
    StringBuffer() : stringstream() {}
    /**
     * Create a string stream with initial contents, underlying
     * stringstream is set to append mode
     *
     * @param initial contents
     */
    StringBuffer(const char* initial)
        : stringstream(initial, ios_base::ate | ios_base::in | ios_base::out)
    {
        // Using GCC the ios_base::ate flag does not seem to have the desired effect
        // As a backup seek the output pointer to the end of buffer
        seekp(0, ios::end);
    }
    /**
     * @return the length of a str held in the underlying stringstream
     */
    long length()
    {
        /*
         * if stream is empty, tellp returns eof(-1)
         *
         * tellp can be used to obtain the number of characters inserted
         * into the stream
         */
        long length = tellp();
        if(length < 0)
            length = 0;
        return length;
    }
};
| 归档时间: | 
 | 
| 查看次数: | 33947 次 | 
| 最近记录: |