2012-10-22 49 views
2

我有一個字符串(一些固定的長度),我需要壓縮然後比較壓縮的長度(作爲數據中的冗餘代理或作爲粗略的近似值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; 

} 
+1

您可以將數據寫入字符串流狀物體?如果是這樣,以字符串形式獲取結果並獲得其長度是微不足道的。 –

回答

4

你可以嘗試添加boost::iostreams::counter到您鏈中的壓縮機之間和水槽,然後調用它的characters()成員獲得通過它去的字節數。

這個工作對我來說:

#include <boost/iostreams/filter/counter.hpp> 

...

io::filtering_ostream out; 
out.push(io::counter()); 
out.push(io::gzip_compressor()); 
out.push(io::counter()); 
out.push(io::file_descriptor_sink("test.gz")); 
out.write (memblock.c_str(), memblock.size()); 
io::close(out); // Needed for flushing the data from compressor 

std::cout << "Wrote " << out.component<io::counter>(0)->characters() << " bytes to compressor, " 
    << "got " << out.component<io::counter>(2)->characters() << " bytes out of it." << std::endl; 
+0

謝謝!它效果很好:) –

1

我想通了另一個(略雨衣)的方式實現字符串的長度被壓縮。我想在這裏分享它,但基本上它只是傳遞無壓縮的字符串過濾緩衝和複製輸出回字符串:

template<typename T> 
inline std::string compressIt(std::vector<T> s){ 

    std::stringstream uncompressed, compressed; 
    for (typename std::vector<T>::iterator it = s.begin(); 
     it != s.end(); it++) 
     uncompressed << *it; 

    io::filtering_streambuf<io::input> o; 
    o.push(io::gzip_compressor()); 
    o.push(uncompressed); 
    io::copy(o, compressed); 

    return compressed.str(); 
} 

後來人們可以很容易地得到壓縮串的大小

compressIt(uncompressedString).size() 

我覺得這樣更好,因爲它不需要我像以前一樣創建輸出文件。

歡呼聲, 尼基爾

0

另一個方法是

stream<array_source> input_stream(input_data,input_data_ize); 
stream<array_sink> compressed_stream(compressed_data,alloc_compressed_size); 
filtering_istreambuf out; 
out.push(gzip_compressor()); 
out.push(input_stream); 
int compressed_size = copy(out,compressed_stream); 
cout << "size of compressed_stream" << compressed_size << endl;