c++ - 使用 POCO 压缩 vector (c++)

标签 c++ stl poco

POCO Library需要一个用于输入的 istream 和用于输出的 ostream 以使用其 zlib 包装器压缩数据。我在 std::vector(unsigned char) 中有数据,想将此数据压缩到另一个 std::vector(unsigned char) 中。有什么简单的方法可以做到这一点吗?

最佳答案

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

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();
}

关于c++ - 使用 POCO 压缩 vector (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4466102/

相关文章:

c++ - 在 C 和 C++ 中包含和链接文件

c++ - STL 算法和 const_iterators

使用 Poco 库的 C++/IOS Websockets

c# - Entity Framework 4.1 POCO - 多对多加载

ssl - HTTPS 使用 Poco C++ 获取 SSL 异常 : OSM Nominatim

c++ - 统一初始化以调用初始化程序列表以外的构造函数

python - 使用 Boost/python.hpp 的 CMake 编译 c++ 程序

c++ - STL for_each 无法在 Xcode 中使用 map

c++ - string::operator+= 问题

c++ - 使用类作为 STL 映射的类型