从const char*创建一个流函数

Create a streambuf from const char*?

本文关键字:一个 函数 const char 创建      更新时间:2023-10-16

我想从原始数据中创建一个只读缓冲区,这样我就可以连续多次调用boost::asio::buffer_copy,而不必担心保持正确的偏移量和长度。

在这种情况下,streambut是正确的解决方案吗?如果是,我如何从const char*原始数据创建流,而不复制它?

既然你正在使用Boost,你可以使用Boost. iostreams .

#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream_buffer.hpp>
int main()
{
    namespace bio = boost::iostreams;
    const char source[] = "hello world";
    bio::stream_buffer<bio::array_source>  stream_buffer(source, sizeof(source));
}