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. Inserting a user defined key in a C++ map

Inserting a user defined key in a C++ map

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
2 Posts 2 Posters 4 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
    Amrit Agr
    wrote on last edited by
    #1

    Hi Guys, I have recently came across one scenario where I have to insert a user defined key in a c++ map. that class looks like this class key { int id; string name; }; map is like map I'll overload "operator<" function and will sort out based on "id" which will works fine, but the condition is like, if there are 2 entry with same ID but with a different name, for ex. {"50", Jack} and {"50", John} As both the values have SAME ID and different name, map should make an entry for both. Only in the case of combination of same ID and same name, it shouldn't allow the entry. bool operator<(key& temp) { if(id < temp.id) { return true; } if( id == temp.id) return name < temp.name; } its not working properly, its overriding the last entry if same ID is there. Please help me out

    D 1 Reply Last reply
    0
    • A Amrit Agr

      Hi Guys, I have recently came across one scenario where I have to insert a user defined key in a c++ map. that class looks like this class key { int id; string name; }; map is like map I'll overload "operator<" function and will sort out based on "id" which will works fine, but the condition is like, if there are 2 entry with same ID but with a different name, for ex. {"50", Jack} and {"50", John} As both the values have SAME ID and different name, map should make an entry for both. Only in the case of combination of same ID and same name, it shouldn't allow the entry. bool operator<(key& temp) { if(id < temp.id) { return true; } if( id == temp.id) return name < temp.name; } its not working properly, its overriding the last entry if same ID is there. Please help me out

      D Offline
      D Offline
      Daniel Pfeffer
      wrote on last edited by
      #2

      Try std::multimap. It does exactly what you want.

      Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

      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