Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. I need help creating a custom 'allocator' for a STL set

I need help creating a custom 'allocator' for a STL set

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11398640
    wrote on last edited by
    #1

    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;

    M 1 Reply Last reply
    0
    • U User 11398640

      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;

      M Offline
      M Offline
      Mike Nordell
      wrote on last edited by
      #2

      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;

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups