创建 C++ 的更简单方法来自 (char*, size_t) 的内存流,而不复制数据?
我找不到任何现成的东西,所以我想出了:
class membuf : public basic_streambuf<char>
{
public:
membuf(char* p, size_t n) {
setg(p, p, p + n);
setp(p, p + n);
}
}
用法:
char *mybuffer;
size_t length;
// ... allocate "mybuffer", put data into it, set "length"
membuf mb(mybuffer, length);
istream reader(&mb);
// use "reader"
我知道 stringstream
,但它似乎无法处理给定长度的二进制数据。
我在这里发明自己的轮子吗?
编辑
- 它不得复制输入数据,而只是创建一些可以迭代数据的东西。
- 它必须是可移植的——至少它应该在 gcc 和 MSVC 下都能工作。
I couldn't find anything ready-made, so I came up with:
class membuf : public basic_streambuf<char>
{
public:
membuf(char* p, size_t n) {
setg(p, p, p + n);
setp(p, p + n);
}
}
Usage:
char *mybuffer;
size_t length;
// ... allocate "mybuffer", put data into it, set "length"
membuf mb(mybuffer, length);
istream reader(&mb);
// use "reader"
I know of stringstream
, but it doesn't seem to be able to work with binary data of given length.
Am I inventing my own wheel here?
EDIT
- It must not copy the input data, just create something that will iterate over the data.
- It must be portable - at least it should work both under gcc and MSVC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设您的输入数据是二进制(不是文本),并且您想从中提取二进制数据块。所有这些都无需复制您的输入数据。
您可以组合
boost::iostreams::basic_array_source
和boost::iostreams::stream_buffer
(来自 Boost.Iostreams)与boost::archive::binary_iarchive
(来自 Boost.Serialization)能够使用方便的提取> >运算符读取二进制数据块。对于 AMD64 上的 GCC 4.4.1,它输出:
Boost.Serialization 非常强大,知道如何序列化所有基本类型、字符串,甚至 STL 容器。您可以轻松地使您的类型可序列化。请参阅文档。隐藏在 Boost.Serialization 源代码中的某个地方是一个可移植二进制存档的示例,它知道如何针对计算机的字节序执行正确的交换。这可能对您也有用。
如果您不需要 Boost.Serialization 的花哨功能并且乐意以 fread() 类型的方式读取二进制数据,则可以以更简单的方式使用
basic_array_source
:我得到相同的输出有了这个程序。
I'm assuming that your input data is binary (not text), and that you want to extract chunks of binary data from it. All without making a copy of your input data.
You can combine
boost::iostreams::basic_array_source
andboost::iostreams::stream_buffer
(from Boost.Iostreams) withboost::archive::binary_iarchive
(from Boost.Serialization) to be able to use convenient extraction >> operators to read chunks of binary data.With GCC 4.4.1 on AMD64, it outputs:
Boost.Serialization is very powerful and knows how to serialize all basic types, strings, and even STL containers. You can easily make your types serializable. See the documentation. Hidden somewhere in the Boost.Serialization sources is an example of a portable binary archive that knows how to perform the proper swapping for your machine's endianness. This might be useful to you as well.
If you don't need the fanciness of Boost.Serialization and are happy to read the binary data in an fread()-type fashion, you can use
basic_array_source
in a simpler way:I get the same output with this program.
我不确定你需要什么,但这能满足你的要求吗?
I'm not sure what you need, but does this do what you want?
标准流缓冲区具有此功能。
创建一个流。获取其缓冲区然后覆盖它。
The standard stream buffer has this functionality.
Create a stream. Gets its buffer then over-ride it.
提问者想要一些不复制数据的东西,他的解决方案工作得很好。我的贡献是稍微清理一下它,这样您就可以创建一个对象作为内存中数据的输入流。我已经测试过这个并且它有效。
The questioner wanted something that doesn't copy the data, and his solution works fine. My contribution is to clean it up a little, so you can just create a single object that's an input stream for data in memory. I have tested this and it works.