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. ATL / WTL / STL
  4. Using HashSet

Using HashSet

Scheduled Pinned Locked Moved ATL / WTL / STL
csharpcomquestion
5 Posts 2 Posters 0 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.
  • A Offline
    A Offline
    Alexandru Savescu
    wrote on last edited by
    #1

    Hello, I want to use the latest collection classes from VC .NET so I chose hash_set. The collection is storing custom objects (not just plain ints) that have operator <. Can anyone show my how do define the hash_set and its corresponding hash_compare correctly? Thanks Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

    J 1 Reply Last reply
    0
    • A Alexandru Savescu

      Hello, I want to use the latest collection classes from VC .NET so I chose hash_set. The collection is storing custom objects (not just plain ints) that have operator <. Can anyone show my how do define the hash_set and its corresponding hash_compare correctly? Thanks Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      You only have to define operator < for your class:

      class MyClass
      {
      public:
      ...
      bool operator<(const MyClass& x)const
      {
      // compare this and x
      }
      };

      Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      A 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        You only have to define operator < for your class:

        class MyClass
        {
        public:
        ...
        bool operator<(const MyClass& x)const
        {
        // compare this and x
        }
        };

        Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        A Offline
        A Offline
        Alexandru Savescu
        wrote on last edited by
        #3

        I wish it were that easy.... I get C2440 error (cannot convert from const MyClass to size_t). I think I must derive from hash_compare and implement custom hash method as the compiler does not know how to get a hash code for my objects... Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

        J 1 Reply Last reply
        0
        • A Alexandru Savescu

          I wish it were that easy.... I get C2440 error (cannot convert from const MyClass to size_t). I think I must derive from hash_compare and implement custom hash method as the compiler does not know how to get a hash code for my objects... Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          This sounds strange. Could you post the code to see how you're using hash_set? I think it should work once you defined operator <. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          A 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            This sounds strange. Could you post the code to see how you're using hash_set? I think it should work once you defined operator <. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            A Offline
            A Offline
            Alexandru Savescu
            wrote on last edited by
            #5

            Here's the code: #include #include using namespace std; class MyClass { public: int x; MyClass (int a) : x (a) { } bool operator < (const MyClass& OB) const { return x < OB.x; } }; int main () { hash_set MySet; MySet.insert (MyClass (2)); } Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

            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