我有一個字符串(一些固定的長度),我需要壓縮然後比較壓縮的長度(作爲數據中的冗餘代理或作爲粗略的近似值Kolmogorov複雜性)。目前,我正在使用boost :: iostreams進行壓縮,這似乎工作得很好。但是,我不知道如何獲得壓縮數據的大小。有人可以幫忙嗎?由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;
}
您可以將數據寫入字符串流狀物體?如果是這樣,以字符串形式獲取結果並獲得其長度是微不足道的。 –