Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can I make QMaps error when you [] on a key not in the map?

Can I make QMaps error when you [] on a key not in the map?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 1.2k Views 2 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.
  • D Offline
    D Offline
    Drew W
    wrote on last edited by
    #1

    If you index a key not in the QMap, Qt automatically creates a new T for you and returns it. This is awful and prone to causing problems because on the surface it looks like everything is fine (no exceptions), but you are getting invalid data which can cause cryptic problems down the line. It also forces you to define a default constructor which may not even be appropriate.

    Right now I've just been making default constructors assert to fix this problem, but sometimes I will use maps for data types that aren't under my direct control. Is there any way for me to force a QMap to throw exceptions when indexing keys not in the map like I get when I go out of range on an iterator/vector, or some alternative data structure that is not flawed like this?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It's not a flaw. You can see that the std::map [] operator does the same thing.

      If you really need something to throw an error, then you can change to std::map and its at method

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by Kent-Dorfman
        #3

        I'd suggest using the non-const version of find() to determine if a key is present or for modifying elements that you know exist.

        {
            mymap::iterator i;
            if (i=map.find(key)) != map.end()) {
               // do something with i
            } else {
                // conditionally do map[key]=value 
                // if insertion is your intention
            }
        }
        

        The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

        1 Reply Last reply
        5
        • GerhardG Offline
          GerhardG Offline
          Gerhard
          wrote on last edited by
          #4

          Hello,

          you can check the existance of a entry with map.count(key) or map.contains()

          Gerhard

          1 Reply Last reply
          3
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            2 solutions:
            If you want to control the default value returned without the missing key being added to the map you can use QMap::value. If you want to error-handle then adopt @Kent-Dorfman 's answer using find()/constFind() to know if an entity exists and operate on it

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved