我知道 vector< bool > 是“邪恶的”,并且 dynamic_bitset 是首选(bitset 不合适),但我使用的是 C++ Builder 6,我真的不想为这样一个旧版本追求 Boost 路线。我试过了:
int RecordLen = 1;
int NoBits = 8;
std::ofstream Binary( FileNameBinary );
vector< bool > CaseBits( NoBits, 0 );
Binary.write( ( const char * ) & CaseBits[ 0 ], RecordLen);
但结果不正确。我怀疑实现可能意味着这是一个愚蠢的尝试,但我不知道。
最佳答案
vector <bool>
的运算符[]不返回引用(因为位不可寻址),因此获取返回值的地址将充满问题。你有没有考虑过std::deque <bool>
?
关于c++ - 我可以将 vector<bool> 的内容直接从内部缓冲区写入流吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2295606/