I have a problem with this code (cubeBoxData is a set of cubeBox):
cubeBox temp(bx,by,bz);
cubeBoxData.insert(temp);
set<cubeBox>::iterator i = cubeBoxData.find(temp);
const_cast<cubeBox&>(*i).addCube(x,y,z);
The problem is that cubeBoxData.find(temp); doesn’t find temp, then the program fails trying to call addCube(), and I don`t know why, because this code works fine (just change the third line):
cubeBox temp(bx,by,bz);
cubeBoxData.insert(temp);
set<cubeBox>::iterator i = find(cubeBoxData.begin(),cubeBoxData.end(),temp);
const_cast<cubeBox&>(*i).addCube(x,y,z);
The operator < for cubeBox is:
bool operator<(const cubeBox& c) const {
return x<c.x ? true : y<c.y ? true : z<c.z ? true : false;
}
And addCube doesn’t change x, y or z.
I think my operator< is wrong and I’m missing something silly, but i can´t figure what is it.
The
operator<that you have defined does not establish a strict weak ordering. For example, according to your comparator, it is both the case that{1,0,1} < {0,1,0}and that{0,1,0} < {1,0,1}. As a result, all of the operations on thesethave undefined behaviour.You should rewrite your comparison operation so that it does establish a strict weak ordering. The easiest(?) way to do this is to use
std::tuple: