std兼容stringstream使用堆栈分配的存储? [英] std compliant stringstream using stack allocated storage?

查看:198
本文介绍了std兼容stringstream使用堆栈分配的存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎找不到任何目前可用的野生,这似乎奇怪。



所以在我滚自己的,有没有人知道一个std兼容stringstream



我正在考虑两种方法来实现这一点:



堆栈上的静态大小的缓冲区(可能是允许编译时定制的模板参数)


  1. 当空间耗尽时,恢复使用动态存储

  2. 当空间耗尽时,使用 alloca 增加堆栈存储空间

这将允许快速输入字符串流中比预定大小更短的字符串。



对于初始大小的合适选择意味着可以在不频繁调整堆大小的情况下实现日志记录。

解决方案

您可以使用 std :: basic_stringbuf< char,char_traits< char>,pooled_allocator> 您只需要编写合并的分配器。



然后只需创建一个 basic_iostream >

或者,从 basic_streambuf 创建一个新类。



但不要重写 stringstream 。 iostreams库是通过替换缓冲区对象的扩展来设计的。


Can't seem to find anything currently available in the wild, which seems odd.

So before I roll my own, does anyone know of a std compliant stringstream which allocates storage on the stack?

I'm thinking of 2 ways to achieve this:

Initially use a statically sized buffer on the stack (probably a template parameter to allow compile-time customization)

  1. When space runs out revert to using dynamic storage
  2. When space runs out use alloca to increase the stack storage

This will allow quick input into a stringstream for strings shorter than the predetermined size.

A suitable choice for the initial size will mean that things like logging can be achieved without frequent resizing from the heap.

解决方案

You can get the same benefits by using std::basic_stringbuf<char, char_traits<char>, pooled_allocator>, where you only have to write the pooled allocator.

Then just create a basic_iostream attached to that buffer.

Or, create a new class derived from basic_streambuf.

But don't rewrite stringstream. The iostreams library is designed by extension by replacing the buffer object.

这篇关于std兼容stringstream使用堆栈分配的存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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