使用 POCO 压缩向量 (c++)

发布于 2024-10-08 08:44:13 字数 209 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2024-10-15 08:44:13

我不知道这是最有效的方法,但作为开始,我会尝试这个:

typedef unsigned char uc;
typedef vector<uc> v;
void Doit(const v& in, v& out)
{
   ostringstream outStream;
   DeflatingOutputStream compressor(outStream, DeflatingStreamBuf::STREAM_GZIP);
   copy(in.begin(), in.end(), ostream_iterator<uc>(compressor));
   compressor.close();
   string outStr(outStream.str());
   out.assign(outStr.begin(), outStr.end());
}

我怀疑这会不必要地复制数据两次。首先,调用 ostringstream::str() 进行复制,然后调用 std::vector::assign() 进行复制。

@Alf P. Steinbach 有一个很好的建议——使用 Boost 流适配器。如果您有可用的 boost::iostreams::filtering_ostream ,您可以尝试以下操作:

typedef unsigned char uc;
typedef vector<uc> v;

void Doit(const v& in, v& out)
{
   filtering_ostream outStream(back_inserter(out));
   DeflatingOutputStream compressor(outStream, DeflatingStreamBuf::STREAM_GZIP);
   std::copy(in.begin(), in.end(), ostream_iterator<uc>(compressor));
   compressor.close();
   outStream.flush();
}

I don't know that this is the most efficient way, but as a start I'd try this:

typedef unsigned char uc;
typedef vector<uc> v;
void Doit(const v& in, v& out)
{
   ostringstream outStream;
   DeflatingOutputStream compressor(outStream, DeflatingStreamBuf::STREAM_GZIP);
   copy(in.begin(), in.end(), ostream_iterator<uc>(compressor));
   compressor.close();
   string outStr(outStream.str());
   out.assign(outStr.begin(), outStr.end());
}

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:

typedef unsigned char uc;
typedef vector<uc> v;

void Doit(const v& in, v& out)
{
   filtering_ostream outStream(back_inserter(out));
   DeflatingOutputStream compressor(outStream, DeflatingStreamBuf::STREAM_GZIP);
   std::copy(in.begin(), in.end(), ostream_iterator<uc>(compressor));
   compressor.close();
   outStream.flush();
}
少跟Wǒ拽 2024-10-15 08:44:13

您可以使用普通的 istream 和 ostream 对象并使用 他们的pubsetbuf方法streambufs 将流的内部缓冲区设置为向量的内部缓冲区。

在这种情况下,您必须确保接收输出的向量有足够的可用空间。由于 ostream 会直接写入缓冲区,因此不会发生自动调整大小。

You could use normal istream and ostream objects and use the pubsetbuf method of their streambufs 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文