查询std::ostringstream内容
Query std::ostringstream content
是否可以在不使用std::ostringstream::str()
成员函数创建要搜索的std::string
的情况下搜索std::ostringstream
的内容?
我有以下内容,并希望避免在每次调用flush_()
时构造std::string
实例:
#include <iostream>
using std::cout;
#include <ios>
using std::boolalpha;
#include <sstream>
using std::ostringstream;
#include <string>
using std::string;
class line_decorating_ostream
{
public:
line_decorating_ostream() { out_ << boolalpha; }
~line_decorating_ostream() { cout << out_.str(); }
template <typename T>
line_decorating_ostream& operator<<(const T& a_t)
{
out_ << a_t;
flush_();
return *this;
}
private:
ostringstream out_;
line_decorating_ostream(const line_decorating_ostream&);
line_decorating_ostream& operator=(const line_decorating_ostream&);
// Write any full lines.
void flush_()
{
string s(out_.str());
size_t pos = s.find('n');
if (string::npos != pos)
{
do
{
cout << "line: [" << s.substr(0, pos) << "]n";
s = s.substr(pos + 1);
} while (string::npos != (pos = s.find('n')));
out_.clear();
out_.str("");
out_ << boolalpha << s;
}
}
};
int main()
{
line_decorating_ostream logger;
logger << "1 " << "2 " << 3 << " 4 " << 5 << "n"
<< "6 7 8 9 10n...n" << true << "n";
return 0;
}
[这并没有引起我担心的任何性能问题,只是想知道这是否可能。]
使用其streambuf
类?还是自己写一本?(好的:你应该写一个自己的:pbase
和pptr
受到保护。)
class my_str_buffer : public basic_stringbuf<char>
{
public:
using basic_stringbuf<char>::pbase;
using basic_stringbuf<char>::pptr;
};
my_str_buffer my_buf;
ostream str( &my_buf );
// do anything
string foo( str.rdbuf()->pbase(), str.rdbuf()->pptr() );
ostream
构造函数和stringbuf
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 你能重载对象变量名本身返回的内容吗
- 如何将内容数组写入文本文件?
- 试图让变量检查数组中的某些内容
- initializer_list中字符串的内容为何为空
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- C++宏忽略之后的内容
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 有没有办法从非C/C++文件中读取C++原始字符串文字的内容
- 如何知道QDataStream不能反序列化某些内容
- 即使我读取了所有内容,在FIFO上打开的QSocketNotifier也会一直启动
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 安装opencv失败-粘贴CMakeError.log的内容
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- gtkmm 显示文件内容
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 打印时有二叉树问题.用户输入不打印任何内容
- 当试图在ostringstream中连接一个字符串时,这些字符串的内容会被修改和重构,字符串会在开始时被添加
- 查询std::ostringstream内容
- ostringstream打印c字符串的地址而不是其内容