私はビットセットを使用するプロジェクトで作業しています。提供されるテキストファイルが非常に大きい(> 800M)ので、std :: bitsetに直接ロードするには25秒以上かかるでしょう。だから、テキストファイルをメモリにバイナリファイルをダンプして前処理したい。 8ビットのcharは1ビットに変換されるため、ファイルの読み込みにかかる時間が大幅に短縮されます。正常に動作思わstd :: bitsetをバイナリファイルに書き込んでstd:bitsetにファイルをロードしてください
#include <iostream>
#include <bitset>
#include <string>
#include <stdexcept>
#include <fstream>
#include <math.h>
int main() {
const int MAX_SIZE = 19;
try {
std::string line = "1001111010011101011";
int copy_bypes = (int)ceil((float)MAX_SIZE/8.0);
std::bitset<MAX_SIZE>* foo = new (std::nothrow)std::bitset<MAX_SIZE>(line); // foo: 0000
std::ofstream os ("data.dat", std::ios::binary);
os.write((const char*)&foo, copy_bypes);
os.close();
std::bitset<MAX_SIZE>* foo2 = new (std::nothrow)std::bitset<MAX_SIZE>();
std::ifstream input("data.dat",std::ios::binary);
input.read((char*)&foo2, copy_bypes);
input.close();
for (int i = foo2->size() -1 ; i >=0 ; --i) {
std::cout << (*foo2)[i];
}
std::cout <<std::endl;
}
catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << '\n';
}
return 0;
}
が、私はこの使用法は本当に生産的環境で正常に動作することができます心配しています:私は、デモ・コードを記述します。
ありがとうございました。
http://stackoverflow.com/questions/5251403/binary-serialization-of-stdbitset – Holt
'std :: bitset * foo = new(std :: nothrow)std ::を参照してください。 bitset (line); ' - なぜあなたはここで動的に割り当てていますか?そして、ポインタのアドレス( '&foo')を' os.write'に与えると、これがどのように「うまくいくか」はわかりません。 –
PaulMcKenzie
*提供されているテキストファイルが非常に大きい(> 800M)ので、std :: bitsetに直接ロードするには25秒以上かかる* - 非常に貧弱なディスクシステムです。 – PaulMcKenzie