Copy a streambuf#39;s contents to a string(将流缓冲的内容复制到字符串)
问题描述
显然 boost::asio::async_read
不喜欢字符串,因为 boost::asio::buffer
的唯一重载允许我创建 const_buffer
s,所以我坚持将所有内容读入流缓冲.
现在我想将 streambuf 的内容复制到一个字符串中,但它显然只支持写入 char* (sgetn()
),使用 streambuf 创建一个 istream 并使用 getline()代码>.
Apparently boost::asio::async_read
doesn't like strings, as the only overload of boost::asio::buffer
allows me to create const_buffer
s, so I'm stuck with reading everything into a streambuf.
Now I want to copy the contents of the streambuf into a string, but it apparently only supports writing to char* (sgetn()
), creating an istream with the streambuf and using getline()
.
有没有其他方法可以在不过度复制的情况下使用streambufs内容创建字符串?
Is there any other way to create a string with the streambufs contents without excessive copying?
推荐答案
我最不喜欢这样的回答:你不想要 X,你想要 Y,这是如何做 Y",但在这种情况下我我很确定我知道 tstenner 想要什么.
I mostly don't like answers that say "You don't want X, you want Y instead and here's how to do Y" but in this instance I'm pretty sure I know what tstenner wanted.
在 Boost 1.66 中,动态字符串添加了缓冲区类型,以便async_read
可以直接调整大小并写入字符串缓冲区.
In Boost 1.66, the dynamic string buffer type was added so async_read
can directly resize and write to a string buffer.
这篇关于将流缓冲的内容复制到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将流缓冲的内容复制到字符串


基础教程推荐
- 这个宏可以转换成函数吗? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01