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. In the Qhash class what does the function "QHash::values("key");" return when the key itself is not found in the Qhash object?

In the Qhash class what does the function "QHash::values("key");" return when the key itself is not found in the Qhash object?

Scheduled Pinned Locked Moved General and Desktop
13 Posts 5 Posters 8.4k Views
  • 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
    andre
    wrote on 6 Dec 2011, 10:52 last edited by
    #2

    It will return an empty QList<T>.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pratik041
      wrote on 6 Dec 2011, 11:22 last edited by
      #3

      [quote author="Andre" date="1323168723"]It will return an empty QList<T>.[/quote]

      So how can i check that it is returning empty QList<T> by code? I have to check this before doing some operation to avoid failure.

      Pratik Agrawal

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on 6 Dec 2011, 11:49 last edited by
        #4

        @
        if(yourHash.contains("key")) {
        // do something
        } else {
        qDebug() << "key not in the hash";
        }
        @

        You did read the [[Doc:QHash]] API docs and just overlooked that method, didn't you?

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pratik041
          wrote on 6 Dec 2011, 11:55 last edited by
          #5

          [quote author="Volker" date="1323172165"]@
          if(yourHash.contains("key")) {
          // do something
          } else {
          qDebug() << "key not in the hash";
          }
          @

          You did read the [[Doc:QHash]] API docs and just overlooked that method, didn't you?[/quote]
          Ya i have read that but just missed .

          Pratik Agrawal

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on 6 Dec 2011, 13:01 last edited by
            #6

            Or, you just test the returned QList:
            @
            QList<T> results = myHash.values("key");
            if (results.isEmpty()) {
            qDebug() << "key not in the hash";
            } else {
            //use the results to do something useful
            }
            @

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fluca1978
              wrote on 6 Dec 2011, 13:33 last edited by
              #7

              [quote author="Andre" date="1323176466"]Or, you just test the returned QList:
              @
              QList<T> results = myHash.values("key");
              if (results.isEmpty()) {
              qDebug() << "key not in the hash";
              } else {
              //use the results to do something useful
              }
              @
              [/quote]

              Better to check if the key is in the hash, because the hash could had been initialized with a key and empty list.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on 6 Dec 2011, 13:40 last edited by
                #8

                [quote author="fluca1978" date="1323178404"]
                Better to check if the key is in the hash, because the hash could had been initialized with a key and empty list.
                [/quote]

                If T is QList<T2>, then method values() returns a list of lists. In the case you described, it would return a QList< QList<T> > containing that empty list :-)

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on 6 Dec 2011, 14:05 last edited by
                  #9

                  [quote author="fluca1978" date="1323178404"]
                  [quote author="Andre" date="1323176466"]Or, you just test the returned QList:
                  @
                  QList<T> results = myHash.values("key");
                  if (results.isEmpty()) {
                  qDebug() << "key not in the hash";
                  } else {
                  //use the results to do something useful
                  }
                  @
                  [/quote]

                  Better to check if the key is in the hash, because the hash could had been initialized with a key and empty list.
                  [/quote]

                  You have a point if you talk about QHash::value() (singular version). In that case, you will get a default-constructed T back, which might be indistinguishable from the value you put in the hash in the first place. However, for QHash::values() (plural version), the difference really is there. An empty list is easy to distinguish from a list with an empty or default-constructed element.

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    veeeee_d
                    wrote on 6 Dec 2011, 15:52 last edited by
                    #10

                    It doesn't matter, Andre. If anything at all is inside the QHash, then the value will be there and the returned QList will have size() 1, it doesn't matter if it is a default value or a value you inserted or if the item is a QList or not.
                    As for the code, efficiency-wise, Volker has the best solution, as it avoids the unnecessary creation of a QList.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on 6 Dec 2011, 16:25 last edited by
                      #11

                      veeeee_d:

                      I tested this code:
                      @
                      QHash<QString, QString> testHash;
                      testHash.insert("foo", "bar");
                      qDebug() << "output size" << testHash.values("testKey").count();
                      @
                      Output is:
                      @
                      output size 0
                      @
                      Meaning that indeed an empty QList will be returned if there is no item with the specified key in the hash.

                      You are right that that object will need to be created, and that takes time. I guess it depends on your use case what the better way is. If in 99% of the cases you expect the key to be in the hash, then I think my version is faster. If you expect that in the majority of cases the key will not be in the hash, I guess checking first will be faster. You'd have to measure the performance to be sure, I think. Remember that the actual lookup of the value isn't free either. For one, it depends on the type of key used how expensive the actual hash function is.

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        veeeee_d
                        wrote on 6 Dec 2011, 16:44 last edited by
                        #12

                        Actually, no. In the specific case you need to find and access the key you are looking for in the QHash, you should use function find(), which returns an iterator to the item, which can then access the item with no additional searching cost.
                        Refer to "this":http://doc.qt.nokia.com/4.7/qhash.html#find function, and you will see the best solution to your suggested problem. The cost of allocating the QList's buffer alone is worse than the whole cost of iterating through find().

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on 7 Dec 2011, 07:35 last edited by
                          #13

                          That is a good suggestion, and is probably the fastest code path. I did not think of using find() in such a scenario.

                          1 Reply Last reply
                          0

                          11/13

                          6 Dec 2011, 16:25

                          • Login

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