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. How to know if the QHash::insert(const Key &key, const T &value) succeeded or failed
Qt 6.11 is out! See what's new in the release blog

How to know if the QHash::insert(const Key &key, const T &value) succeeded or failed

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.9k 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.
  • E Offline
    E Offline
    eMan.Lived
    wrote on last edited by
    #1

    The documentation says nothing about this.

    Venkatesh VV jsulmJ 2 Replies Last reply
    0
    • E eMan.Lived

      The documentation says nothing about this.

      Venkatesh VV Offline
      Venkatesh VV Offline
      Venkatesh V
      wrote on last edited by
      #2

      @eMan.Lived
      Hi,

      Based on QHash size you can know value is inserted or not.
      if inserted size becomes +1 to previous size if not size remains same as previous size.

      int mapSize;
      QHash<key,value> hash;
      mapSize = hash.size();

      hash.insert(key,Value);
      if(hash.size()==mapSize+1)
      {
      //inserted succeeded
      mapSize = hash.size();

      }else{
      //insertFailed
      }

      1 Reply Last reply
      2
      • E eMan.Lived

        The documentation says nothing about this.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @eMan.Lived insert() returns an iterator. I'm not sure (and documentation really does not say anything about it!), but I think if insertion fails the iterator will be hash.end(), otherwise it will point to the just inserted pair. You can check Qt source code to find out what really happens.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • E Offline
          E Offline
          eMan.Lived
          wrote on last edited by eMan.Lived
          #4

          @Venkatesh-V
          Hello,
          It looks like the developers have forgotten something. I mean that it looks more clear like this:

          QHash<key, value> hash;
          if (hash.insert(Key, Value) == hash.end())
          {
            // failed
          }
          else
          {
            // succeeded
          }
          

          Well, thanks for the workaround.

          @jsulm
          Hello,
          I've looked source code.

          1. QHash::insert() just return result of called QHash::insertMulti() (line 1011).
          2. QHash::insertMulti(), in turn, returns an iterator, in the constructor of which the result of calling function createNode() is passed (line 781).
          3. createNode() returns a pointer to the Node created by the new operator (lines 548, 551).

          As you can see there is no check for successful memory allocation. Maybe throws an exception?

          jsulmJ 1 Reply Last reply
          0
          • E eMan.Lived

            @Venkatesh-V
            Hello,
            It looks like the developers have forgotten something. I mean that it looks more clear like this:

            QHash<key, value> hash;
            if (hash.insert(Key, Value) == hash.end())
            {
              // failed
            }
            else
            {
              // succeeded
            }
            

            Well, thanks for the workaround.

            @jsulm
            Hello,
            I've looked source code.

            1. QHash::insert() just return result of called QHash::insertMulti() (line 1011).
            2. QHash::insertMulti(), in turn, returns an iterator, in the constructor of which the result of calling function createNode() is passed (line 781).
            3. createNode() returns a pointer to the Node created by the new operator (lines 548, 551).

            As you can see there is no check for successful memory allocation. Maybe throws an exception?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @eMan.Lived Well, in case memory cannot be allocated there will be an exception and the application will be closed by the OS.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3

            • Login

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