[英]Bitwise operations on SINGLE BIT of std::vector<bool>. Why the error?
I have come up with a set of rules to transform one set of bits into another.我想出了一组规则来将一组位转换为另一组。
For these to work, I basically need to xor a certain number of bits in the source set of bits (say, result[i]=source[foo(i)]^source[bar(i)]
, where foo
and bar
are bounds-checked).为了使这些工作,我基本上需要对源位组中的一定数量的位进行异或(例如,
result[i]=source[foo(i)]^source[bar(i)]
,其中foo
和bar
是边界检查)。 Since I want to be able to change the size of the set, I decided to go with a std::vector<bool>
.由于我希望能够更改集合的大小,因此我决定使用
std::vector<bool>
。
Thus, I end up with:因此,我最终得到:
int foo(int i);
int bar(int i);
void baz(std::vector<bool> in, std::vector<bool>& out){
out.clear();
for(int i=0;i<in.size();i++){
if(foo(i)>0 && foo(i)<in.size())
out[i]^=in[foo(i)]
if(bar(i)>0 && bar(i)<in.size())
out[i]^=in[bar(i)]
}
}
However, this gives me an error:但是,这给了我一个错误:
No viable overloaded ^=
没有可行的重载 ^=
What can I do in order to be able to do this stuff?为了能够做这些事情,我能做什么?
std::vector<bool>
is messy in the best case and an abomination in the worst. std::vector<bool>
在最好的情况下是混乱的,在最坏的情况下是令人憎恶的。 Its operator[]
does not return a bool&
but rather a proxy object ( see here ).它的
operator[]
不返回bool&
而是返回一个代理对象( 见这里)。 This proxy does not have overloads vor all possible bitwise operations, hence you have to use ifs, its operator=(bool)
or its flip()
.此代理没有重载或所有可能的按位运算,因此您必须使用 ifs、其
operator=(bool)
或它的flip()
。
EDIT: I just read your code and not your description what you want to do.编辑:我只是读了你的代码,而不是你的描述你想做什么。 Setting the xor of two bits is no problem at all:
设置两位的异或完全没有问题:
out[i] = in[j] ^ in[k]; // Whatever the right indices are.
I think what you actually want to do is along the lines of:我认为你真正想做的是:
bool tmp = false;
if(foo(i)>0 && foo(i)<in.size())
tmp^=in[foo(i)];
if(bar(i)>0 && bar(i)<in.size())
out[i]=tmp^in[bar(i)];
That is, if you want to return in[bar(i)]^in[foo(i)]
if both pass the bounds check, return in[j]
if only one (here denoted by j
) passes the bounds check and false
if none of them pass it.也就是说,如果你想返回
in[bar(i)]^in[foo(i)]
如果两者都通过边界检查,则返回in[j]
如果只有一个(这里用j
表示)通过边界检查并且false
如果他们都没有通过。
std::vector<bool>
is a special container. std::vector<bool>
是一个特殊的容器。 It's like a std::vector<int>
, but the standard allows for std::vector<bool>
to pack its elements into single bits which means it returns a proxy object when you use operator[]
.它就像一个
std::vector<int>
,但标准允许std::vector<bool>
将其元素打包成单个位,这意味着当您使用operator[]
时它返回一个代理对象。 That object doesn't have an overload for operator ^=
so you can't use it.该对象没有
operator ^=
的重载,因此您无法使用它。 What you can do is write out the long form of ^=
like您可以做的是写出
^=
的长形式,例如
out[i] = out[i] ^ in[foo(i)]
std::vector<bool>::operator[]
returns a proxy class. std::vector<bool>::operator[]
返回一个代理类。 You can see the documentation on cppreference .您可以查看有关cppreference的文档。
You can use static_cast<bool>
so get an actual bool value and use that in your XOR.您可以使用
static_cast<bool>
来获取实际的 bool 值并在 XOR 中使用它。
out[i] = static_cast<bool>(out[i]) ^ in[foo(i)];
Edit: Or as pointed out by Max, simply rewriting it will do an implicit conversion so the cast is redundant.编辑:或者正如 Max 所指出的那样,只需重写它就会进行隐式转换,因此强制转换是多余的。
out[i] = out[i] ^ in[foo(i)];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.