尝试更改向量 bool 中的值
我正在阅读 Stroustrup PPP 并在做练习时发现了这个错误。
为什么我无法更改矢量布尔值?
#include "std_lib_facilities.h"
int main()
{
vector<bool> vecc(2, true);
vecc[1] = true; // I can't do this, returns error.
return 0;
}
错误:
错误:无法绑定“bool&”类型的非常量左值引用to an rvalue of type 'bool' return std::vector
其次,当我尝试使用 int 类型时,问题不会发生。
#include "std_lib_facilities.h"
int main(){
vector<int> vecc = {true, true};
for(bool x : vecc) {cout<<x<<"\n";}
vecc[1] = false; //ok
for(bool x : vecc){cout<<x<<"\n";}
return 0;
}
输出:
1
1
1
0
编辑:
可能这是因为我正在使用 #include "std_lib_facilities.h"
I'm reading Stroustrup PPP and while doing an exercise, I found this error.
Why I can't change the value of a vector bool?
#include "std_lib_facilities.h"
int main()
{
vector<bool> vecc(2, true);
vecc[1] = true; // I can't do this, returns error.
return 0;
}
The error:
error: cannot bind non-const lvalue reference of type 'bool&' to an rvalue of type 'bool' return std::vector<T>::operator[](i);
Second, when I try with the int type, the problem does not occur.
#include "std_lib_facilities.h"
int main(){
vector<int> vecc = {true, true};
for(bool x : vecc) {cout<<x<<"\n";}
vecc[1] = false; //ok
for(bool x : vecc){cout<<x<<"\n";}
return 0;
}
Output:
1
1
1
0
Edit:
Probably this is because I was using #include "std_lib_facilities.h"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以添加一些错误检查功能,以帮助人们开始其编程生涯:
std_lib_facilities.h 定义了以下帮助程序模板, operator[](unsigned int i) 及其
const
变体返回对T
的引用,在本例中T
是布尔。为了获取它调用
std::vector
的operator[]
的元素来完成繁重的工作。不幸的是
std::vector
不是一个合适的库容器。由于它打包位而不是存储bool
,因此它无法从元素访问方法返回对bool
的引用。它没有任何可供引用的bool
。 相反,它返回一个隐藏了收拾行李远离视线,通常会让生活变得更轻松一些。在大多数情况下,元素访问方法的行为与常规向量
完全相同。但这次不是。它不返回引用,也不返回对 bool 的引用,因此简单的传递无法编译。
常见的解决方法是使用包含一些小整数类型或
enum
的std::vector
或使用std::deque
其使用方式大致相同,并且没有位支持专门化。如果大小在编译时已知,请考虑使用std::bitset< /代码>
。
std_lib_facilities.h defines the following helper template to add a bit of error checking helpful to folks beginning their programming careers:
T& operator[](unsigned int i)
and itsconst
variant return a reference toT
and in this caseT
isbool
. To get the element it calls through tostd::vector
'soperator[]
to do the grunt work.Unfortunately
std::vector<bool>
is not a proper library container. Because it packs bits rather than storingbool
s, it can't return a reference to abool
from the element access methods. It doesn't have anybool
s to reference. Instead it returns a groovyreference
class that conceals the bit packing from sight and usually makes life a bit easier. Under most circumstances the element access methods will behave exactly the same way as a regularvector
.But not this time. It's not returning a reference and it's not returning a reference to a
bool
so the simple pass-through fails to compile.Common work arounds are to use a
std::vector
containing some small integer type or anenum
or use astd::deque<bool>
which can be used largely the same way and doesn't have a bit-backing specialization. If the size is known at compile time, consider using astd::bitset
.标准库为 bool 类型提供了 std::vector 的专门化,这可以针对空间效率进行优化。这会将 bool 存储为 1 位,为大型向量节省大量内存。
问题是您无法返回对位的引用,因此标准库应该返回允许分配的位周围的
引用
对象包装器。该对象保留对向量和您要求的位位置的引用,并有一个调用std::vector::assign()
的operator=()
。示例代码甚至是这样的:
我唯一能想到的是你的编译器有一个旧版本的 STL 或一个糟糕的实现,它确实有向量专门化但没有包装器引用对象。尝试
The standard library provides a specialization of std::vector for the type bool, which may be optimized for space efficiency. This stores bool as 1 bit saving you a lot of memory for large vectors.
The problem with that is that you can not return a reference to a bit so the standard library is supposed to return
reference
object wrapper around the bit that allows assignment. The object keeps a reference to the vector and the bit position you asked for and has anoperator=()
that callsstd::vector<bool>::assign()
.The example code for this even has this:
Only thing I can think of is that your compiler has an old version of the STL or a bad implementation that does have a vector specialization but without the wrapper reference object. Try