我需要补充 的价值观std::vector<bool>。所以我想到使用 range for 循环通过引用获取元素。但编译器给了我以下错误
error: cannot bind non-const lvalue reference of type 'std::_Bit_reference&' to an rvalue of type 'std::_Bit_iterator::reference'
13 | for (auto& bit : rep)
Run Code Online (Sandbox Code Playgroud)
这是我的示例代码
#include <iostream>
#include <vector>
int main() {
std::vector<bool> rep;
rep.push_back(true);
rep.push_back(false);
rep.push_back(true);
rep.push_back(false);
rep.push_back(true);
rep.push_back(false);
for (auto& bit : rep)
bit = !bit;
}
Run Code Online (Sandbox Code Playgroud)