I need help creating a custom 'allocator' for a STL set
-
I'm trying to create a set that stores vertices where vertices are defined by this class:
Quote:
class Vertex { public: Vertex(); ~Vertex(); Vertex(double x_set, double y_set, double z_set, int bezierInd); double x,y,z; int bezierIndex; bool operator<(const Vertex& rhs); bool operator==(const Vertex& rhs); bool operator!=(const Vertex& rhs); };
I don't really know if i need a an allocator, but compile errors tell me I do. Also do I need a custom comparator? I created this one :
Quote:
struct vertexCmp { bool operator()(const Vertex& a, const Vertex& b) const { return a.bezierIndex < b.bezierIndex; } };
and I'm creating a set like this :
set<Vertex, vertexCmp> bezierPoints;
-
I'm trying to create a set that stores vertices where vertices are defined by this class:
Quote:
class Vertex { public: Vertex(); ~Vertex(); Vertex(double x_set, double y_set, double z_set, int bezierInd); double x,y,z; int bezierIndex; bool operator<(const Vertex& rhs); bool operator==(const Vertex& rhs); bool operator!=(const Vertex& rhs); };
I don't really know if i need a an allocator, but compile errors tell me I do. Also do I need a custom comparator? I created this one :
Quote:
struct vertexCmp { bool operator()(const Vertex& a, const Vertex& b) const { return a.bezierIndex < b.bezierIndex; } };
and I'm creating a set like this :
set<Vertex, vertexCmp> bezierPoints;
You don't need a custom allocator for that type (or frankly, any type - effectively). Custom allocators are a way to speed things up, or to keep data localized (definitely not mutually exclusive goals). You DO however definitely need custom comparator functions, and you are close to getting it right. Very close. Have you considered making the member comparators const themselves, e.g. "bool operator<(const Vertex& rhs) const;"?. ;-P ++luck;