I'm trying to exploit the fact that a vector of bools condenses storage to a single bit. The following recipe should be a good way to determine whether an int array contains repeats (Or is it? Is something inherently flawed about my whole idea?). I can't figure out why my compiler, XCode, is not liking the
INT_MAX - INT_MIN + 1
line of the following code. I tried casting the expression to a long, but I got the same warning. Any help is greatly appreciated!
bool contains_repeats_3(const std::vector<int>& V) {
std::vector<bool> bv (INT_MAX - INT_MIN + 1, 0); // <------ That's the problem line
for (std::vector<int>::const_iterator it = V.begin() ; it != V.end(); ++it) {
if (bv[*it - INT_MIN] == 1) {
return true;
} else {
bv[*it - INT_MIN] == 1;
}
}
return false;
}
INT_MAX - INT_MIN + 1? Isn't it always0theoretically?0theoretically?INT_MINis negative. So?std::vector<bool>in C++ is already specialized to use just one bit per item?