c++ - 使用 bool 作为左值

标签 c++ iterator int boolean dereference

类型int时编译正常,但当类型为<时给出“错误C2102:'&'需要左值”强> boolean 。为什么?

std::vector<Type> Vector;
Vector.push_back(false);
Vector.push_back(true);
Vector.push_back(true);

const void* Pointer = NULL;
for (std::vector<Type>::const_iterator it = Vector.begin(); it != Vector.end(); ++it)
    Pointer = &(*it);

最佳答案

vector专门用于bool ,并且在该专业领域中,*it不是左值。不可能指向个人bool s 在 vector 内。

事后看来,这种特化被认为是一个错误。现在通常建议避免使用 vector<bool> 。替代方案包括std::bitset .

关于c++ - 使用 bool 作为左值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34016340/

相关文章:

c++ - 字符串数组的内存分配

c++ - 使用迭代器从 vector 中查找子字符串

c++ - 具有整数溢出的递归斐波那契

ruby - File.open ('file.txt' ) 与 File.open ('file.txt' ).readlines

python - 如何以特定方式迭代字典?

arrays - Swift 将 String 数组转换为 Int 数组

python - 在 Python 3 中将整数的字符串表示形式编码为 base64

c++ - while 循环中的 MySQL Connector/C++ 错误

c++ - 文本来自 QPushButton 时如何启用 QLineEdit 编辑?

c++ - 什么时候需要修改socket的接收缓冲区大小?