我有一个字符串(一些固定长度),我需要对其进行压缩,然后比较压缩后的长度(作为数据冗余的代理或作为 Kolmogorov 复杂度的粗略近似值)。目前,我正在使用 boost::iostreams 进行压缩,这似乎运作良好。但是,我不知道如何获取压缩数据的大小。有人可以帮忙吗?
代码片段是
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/filesystem.hpp>
#include <string>
#include <sstream>
namespace io = boost::iostreams;
int main() {
std::string memblock;
std::cout << "Input the string to be compressed:";
std::cin >> memblock;
std::cout << memblock << std::endl;
io::filtering_ostream out;
out.push(io::gzip_compressor());
out.push(io::file_descriptor_sink("test.gz"));
out.write (memblock.c_str(), memblock.size());
std::cout << out.size() << std::endl;
return 0;
}