vendredi 25 novembre 2016

C++: find() with custom == operator

I have a case where apparently no operator== is defined for external class and I need to use a find() function. I know I could do this step by step, but I wonder - is there a way to define custom == operator for this find function, similar how we define Hash function for unordered_set? The case:

#include <vector>
#include <cstdlib>
#include <MathGeoLib/Math/float3.h>

bool operator==(const math::float3 &lhs, const math::float3 &rhs){
   return lhs.x == rhs.x;
}

int main(){
   std::vector<math::float3> V;
   ...
   std::find(V.begin(),V.end(),math::float3(0,0,0));
}

returns

binary '==': no operator found which takes a left-hand operand of type math::float3' (or there is no acceptable conversion)

Sometimes I would like to find not exact same vector, but vector close enough - here I would just override operator== with more suitable. Is there any smart way to do that?

Aucun commentaire:

Enregistrer un commentaire