C++ std::set custom comparator -
well, problem i'm using std::set custom comparator, like:
class { 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, use like
std::set<a*, comp> myset; // insert data a* = new a(2,1); a* b = new a(1,3); myset.insert(a); myset.insert(b);
now problem this:
myset.find( (2 << 16) | 1 );
but, of course, excepts a* not short int.
so, know use std::find_if, won't render useless custom comparator? iterate whole list, wouldn't it? there way use find hashcode rather object itself?
thank you!
set::find
takes argument of type key_type
(see discussion why set::find not template?). using std::set have construct temporary object use find
.
myset.find(a(2, 1));
if not cheap construct might want use std::map<int, a>
(or wrapper around it) instead.
Comments
Post a Comment