POCO Library은 zlib 래퍼를 사용하여 입력을 위해 istream을, 출력을 위해 ostream을 사용하여 데이터를 압축해야합니다. std :: vector (unsigned char)에 데이터가 있고이 데이터를 다른 std :: vector (unsigned char)로 압축하려고합니다. 이 작업을 수행하는 쉬운 방법이 있습니까?POCO (C++)로 벡터 압축하기
1
A
답변
2
나는 이것이 가장 효율적인 방법입니다 모르겠어요,하지만 시작으로 나는이 시도 거라고는 :
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::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();
}
2
보통 istream
및 ostream
개체를 사용하고 메서드를 their streambuf
s으로 사용하여 스트림의 내부 버퍼를 벡터의 내부 버퍼로 설정할 수 있습니다.
이 시나리오에서는 출력을받는 벡터에 충분한 공간이 있는지 확인해야합니다. ostream이 버퍼에 직접 쓰기 때문에 자동 크기 조정이 수행되지 않습니다.
대부분 스트림 어댑터를 제공합니다. 그렇지 않다면 Boost가 가장 가능성이 높습니다. 설명서를 읽으십시오. –