Well, my problem is that I'm using an std::set with a custom comparator, something like:
class A
{
public:
A(int x, int y):
_x(x), _y(y)
{
}
int hashCode(){ return (_y << 16) | _x; }
private:
short int _y;
short int _x;
};
struct comp
{
bool operator() (A* g1, A* g2) const
{
return g1->hashCode() < g2->hashCode();
}
};
So, I use it like
std::set<A*, comp> myset;
// Insert some data
A* a = new A(2,1);
A* b = new A(1,3);
myset.insert(a);
myset.insert(b);
Now my problem is that I would like to do this:
myset.find( (2 << 16) | 1 );
But, of course, it excepts A* not short int.
So, I know I could use std::find_if, but won't it render useless the custom comparator? It would iterate the whole list, wouldn't it? Is there any way I could use find with the hashCode rather than the object itself?
Thank you!
set::find
takes argument of type key_type
(see discussion Why is set::find not a template?). Using a std::set you have to construct a temporary object to use find
.
myset.find(A(2, 1));
If A is not cheap to construct you might want to use a std::map<int, A>
(or a wrapper around it) instead.