I am using a std::set to implement a specific algorithm. The set had duplicates in it, so I assume I had to overload the operator. The overload looks like this.
class Vec3f {
...
bool operator () ( const Vector3f& v0, const Vector3f& v1 ) const {
float epsilon = 1e-7;
return ((v1[0] - v0[0]) > epsilon) && ((v1[1] - v0[1]) > epsilon) && ((v1[2] - v0[2]) > epsilon);
} ...
"Vec3f.h"
int main(){
...
std::set<Vec3f,Vec3f> visited;
...
}
I overloaded it so I could use the < operator needed in std::set. This function returns true if v0 < v1 to some margin. It removes the duplicates, but it also removes valid values in the set. I know my set is supposed have to 12 Vec3fs. With the duplicates, it has 24 Vec3fs. With my comparison function, it only has 3 Vec3f. I considered using absolute difference, but that violates strict weak ordering criterion. My question is: how do I write the comparison function to remove duplicates and keepd only the unique items?
As you have it now, every component in
v0
needs to be less than every component inv1
. This is not a strict weak ordering. You should check one component at a time. Only checking subsequent components if the component you are currently checking is equal. Also, you should drop the epsilon. While that's useful for checking equivalence of results of floating point calculations, it is not useful for ordering, because it too violates strict weak ordering. The easiest way to do this is to useoperator<
forstd::tuple
Otherwise, if you want to implement this manually, it's something like this: