c++ - 我可以将 vector<bool> 的内容直接从内部缓冲区写入流吗?

标签 c++ vector standards boolean

我知道 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/

相关文章:

c++ - 在 std::conditional 中使用模板来确定函数参数类型

c++ - sqlite CREATE INDEX exec 返回错误

c++ - 删除 vector 中携带的对象?

standards - W3C 文档状态

c++ - CL.exe 在 Visual Studio 2012 中崩溃

c++ - 将 C++ 字符数组自动转换为字符串

c++ - 使用比较函数对对象 vector 进行排序

c++ - 在使用模板类制作自定义 vector 时,我应该如何处理析构函数?

css - 设置标签字段表单样式的最佳方式?

c++ - 整数除法溢出