Saraph
Saraph

Reputation: 1002

Check if vector of bools (range) equals to sequence of 1 and 0

What I had before was a string of 1s and 0s and I could simply check whether some range equals to a sequence I wanted:

if (myString.substr(0, 4) == "1110") ...

For memory reasons, I've made this string into vector<bool>, since one bool in vector only takes 1 bit instead of 1 byte.

Now, here's a problem. I want to do same comparison as I did with substr. Possibly without anything like:

if(myVector[0] == true && myVector[1] == true && ...)

or

vector<bool> tmpVector;
tmpVector.push_back(true);
tmpVector.push_back(true);
...
if (myVector == tmpVector) ...

Is there a more elegant solution to this?

Upvotes: 4

Views: 96

Answers (1)

timrau
timrau

Reputation: 23058

bool tmpVector[4] = {true, true, true, false};
if (std::equal(myVector.begin(), myVector.begin() + 4, tmpVector)) {
}

Upvotes: 5

Related Questions