簡體   English   中英

通過boost :: iostreams壓縮的字符串長度

[英]compressed length of a string by boost::iostreams

我有一個字符串(長度固定),需要壓縮然后比較壓縮后的長度(作為數據冗余的代理或作為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;

}

您可以嘗試將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;

我想出了另一種(略微閃爍的)方式來實現字符串的壓縮長度。 我以為在這里共享它,但基本上,它只是將未壓縮的字符串傳遞給過濾的緩沖區,然后將輸出復制回字符串:

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

我感覺更好,因為它不需要像以前那樣創建輸出文件。

干杯,尼基爾

另一種方式是

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM