使用 POCO 压缩向量 (c++)
POCO 库 需要 istream 用于输入,ostream 用于输出,以使用其 zlib 包装器压缩数据。我有 std::vector(unsigned char) 中的数据,并希望将此数据压缩到另一个 std::vector(unsigned char) 中。有什么简单的方法可以做到这一点吗?
The POCO Library requires an istream for input and ostream for output to compress data using its zlib wrapper. I have data in a std::vector(unsigned char) and would like to compress this data into another std::vector(unsigned char). Is there any easy way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这是最有效的方法,但作为开始,我会尝试这个:
我怀疑这会不必要地复制数据两次。首先,调用 ostringstream::str() 进行复制,然后调用 std::vector::assign() 进行复制。
@Alf P. Steinbach 有一个很好的建议——使用 Boost 流适配器。如果您有可用的
boost::iostreams::filtering_ostream
,您可以尝试以下操作:I don't know that this is the most efficient way, but as a start I'd try this:
I suspect this copies the data twice, unnecessarily. First, the call to ostringstream::str() makes a copy, and next std::vector::assign() makes a copy.
@Alf P. Steinbach had an excellent suggestion -- use Boost stream adapters. If you have
boost::iostreams::filtering_ostream
available to you, you could try this:您可以使用普通的 istream 和 ostream 对象并使用 他们的
pubsetbuf
方法streambuf
s 将流的内部缓冲区设置为向量的内部缓冲区。在这种情况下,您必须确保接收输出的向量有足够的可用空间。由于 ostream 会直接写入缓冲区,因此不会发生自动调整大小。
You could use normal
istream
andostream
objects and use thepubsetbuf
method of theirstreambuf
s to set the internal buffer of the stream to the internal buffer of the vector.In this scenario you would have to make sure that the vector receiving the output has enough space available. Since the ostream would write directly to the buffer, no automatic resizing would happen.