我之前拥有的是一串 1 和 0,我可以简单地检查某个范围是否等于我想要的序列:
if (myString.substr(0, 4) == "1110") ...
出于内存原因,我将这个字符串变成了vector<bool>
, 因为 vector 中的一个 bool 只占用 1 位而不是 1 字节。
现在,这里有一个问题。我想做与 substr
相同的比较.可能没有类似的东西:
if(myVector[0] == true && myVector[1] == true && ...)
或
vector<bool> tmpVector;
tmpVector.push_back(true);
tmpVector.push_back(true);
...
if (myVector == tmpVector) ...
有没有更优雅的解决方案?
最佳答案
bool tmpVector[4] = {true, true, true, false};
if (std::equal(myVector.begin(), myVector.begin() + 4, tmpVector)) {
}
关于c++ - 检查 bool vector (范围)是否等于 1 和 0 的序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22354408/