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. List of key-value pairs that can return both keys and values

List of key-value pairs that can return both keys and values

Scheduled Pinned Locked Moved Solved General and Desktop
qlistqbluetoothcontainers
14 Posts 6 Posters 2.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.
  • C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 25 Mar 2020, 14:18 last edited by
    #2

    Simply use a QMap or QHash

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    S 1 Reply Last reply 25 Mar 2020, 14:26
    3
    • C Christian Ehrlicher
      25 Mar 2020, 14:18

      Simply use a QMap or QHash

      S Offline
      S Offline
      SpaceToon
      wrote on 25 Mar 2020, 14:26 last edited by
      #3

      @Christian-Ehrlicher said in List of key-value pairs that can return both keys and values:

      Simply use a QMap or QHash

      Thank you. So I would have something like:

      QMap<QString, QBluetoothUuid> dataMap;
      
      dataMap["Device Information Service"] = QBluetoothUuid(QUuid(6e400003-b5a3-f393-e0a9-e50e24dcca9e));
      
      /*Retrieving the Uuid that belongs to my Device Information Service:*/
      
      qDebug() << dataMap["Device Information Service"]; //output Should be {6e400003-b5a3-f393-e0a9-e50e24dcca9e}
      

      But how Can I retrieve the value that belongs to the specified Uuid? Because I think that this will not work:

      qDebug() << dataMap[QBluetoothUuid(QUuid(6e400003-b5a3-f393-e0a9-e50e24dcca9e))];
      
      P 1 Reply Last reply 25 Mar 2020, 15:52
      0
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 25 Mar 2020, 15:09 last edited by
        #4

        @SpaceToon said in List of key-value pairs that can return both keys and values:

        But how Can I retrieve the value that belongs to the specified Uuid?

        Did you actually read my links? There are a lot of functions...

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • S SpaceToon
          25 Mar 2020, 14:26

          @Christian-Ehrlicher said in List of key-value pairs that can return both keys and values:

          Simply use a QMap or QHash

          Thank you. So I would have something like:

          QMap<QString, QBluetoothUuid> dataMap;
          
          dataMap["Device Information Service"] = QBluetoothUuid(QUuid(6e400003-b5a3-f393-e0a9-e50e24dcca9e));
          
          /*Retrieving the Uuid that belongs to my Device Information Service:*/
          
          qDebug() << dataMap["Device Information Service"]; //output Should be {6e400003-b5a3-f393-e0a9-e50e24dcca9e}
          

          But how Can I retrieve the value that belongs to the specified Uuid? Because I think that this will not work:

          qDebug() << dataMap[QBluetoothUuid(QUuid(6e400003-b5a3-f393-e0a9-e50e24dcca9e))];
          
          P Offline
          P Offline
          Pablo J. Rogina
          wrote on 25 Mar 2020, 15:52 last edited by Pablo J. Rogina
          #5

          @SpaceToon said in List of key-value pairs that can return both keys and values:

          QMap<QString, QBluetoothUuid> dataMap;

          Please keep in mind that a QMap (or QHash) is a ONE way container: you can only look up by key.

          If you want to look up also by value, i.e. do a reverse lookup, you'll need a "bimap", and you have 2 options I guess:

          1. use external support for such structure, i.e. Boost Bimap
          2. use another QMap (or QHash) to use the device Id as key
          QMap<QBluetoothUuid, QString> deviceIdMap;
          

          Using option #2 requires that you need to maintain 2 containers at the same time, i.e. if you need to remove device Id "12345" then you need to remove as well the entry in the other container matching that device Id.

          Edit: you may want to look at this Stackoverflow Q&A

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          2
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 25 Mar 2020, 16:08 last edited by
            #6

            @Pablo-J-Rogina said in List of key-value pairs that can return both keys and values:

            you can only look up by key.

            No, you can also look up by value, see the docs. Although it's not very optimal but the OP did not tell us how much entries he has. For a small value it's negligible.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            P 1 Reply Last reply 25 Mar 2020, 16:33
            2
            • C Christian Ehrlicher
              25 Mar 2020, 16:08

              @Pablo-J-Rogina said in List of key-value pairs that can return both keys and values:

              you can only look up by key.

              No, you can also look up by value, see the docs. Although it's not very optimal but the OP did not tell us how much entries he has. For a small value it's negligible.

              P Offline
              P Offline
              Pablo J. Rogina
              wrote on 25 Mar 2020, 16:33 last edited by
              #7

              @Christian-Ehrlicher said in List of key-value pairs that can return both keys and values:

              No, you can also look up by value, see the docs

              Could you please elaborate about that? I couldn't find it.

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              C 1 Reply Last reply 25 Mar 2020, 16:39
              0
              • P Pablo J. Rogina
                25 Mar 2020, 16:33

                @Christian-Ehrlicher said in List of key-value pairs that can return both keys and values:

                No, you can also look up by value, see the docs

                Could you please elaborate about that? I couldn't find it.

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 25 Mar 2020, 16:39 last edited by
                #8

                @Pablo-J-Rogina
                https://doc.qt.io/qt-5/qmap.html#key
                https://doc.qt.io/qt-5/qhash.html#key

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                P 1 Reply Last reply 25 Mar 2020, 18:18
                6
                • C Christian Ehrlicher
                  25 Mar 2020, 16:39

                  @Pablo-J-Rogina
                  https://doc.qt.io/qt-5/qmap.html#key
                  https://doc.qt.io/qt-5/qhash.html#key

                  P Offline
                  P Offline
                  Pablo J. Rogina
                  wrote on 25 Mar 2020, 18:18 last edited by
                  #9

                  @Christian-Ehrlicher thank you for the enlightment...
                  It was just the habit of using key as the lookup approach.
                  Anyways, I guess the OP has everything at hand to solve the issue.

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  2
                  • S Offline
                    S Offline
                    SpaceToon
                    wrote on 26 Mar 2020, 10:49 last edited by SpaceToon
                    #10

                    Thank you both very much. I have 20 key-value-pairs, the values ​​are constant, no more are added and none are removed or changed.
                    @Christian-Ehrlicher do you think, that 20 pairs are too much so that the QMap would be slow?

                    S P 2 Replies Last reply 26 Mar 2020, 12:25
                    0
                    • S SpaceToon
                      26 Mar 2020, 10:49

                      Thank you both very much. I have 20 key-value-pairs, the values ​​are constant, no more are added and none are removed or changed.
                      @Christian-Ehrlicher do you think, that 20 pairs are too much so that the QMap would be slow?

                      S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 26 Mar 2020, 12:25 last edited by
                      #11

                      Hi,

                      @SpaceToon said in List of key-value pairs that can return both keys and values:

                      Thank you both very much. I have 20 key-value-pairs, the values ​​are constant, no more are added and none are removed or changed.
                      @Christian-Ehrlicher do you think, that 20 pairs are too much so that the QMap would be slow?

                      If you are having speed issue with 20 elements then it's likely somewhere else.

                      What makes you think that a slowdown may happen ?

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

                      S 1 Reply Last reply 26 Mar 2020, 16:47
                      3
                      • S SpaceToon
                        26 Mar 2020, 10:49

                        Thank you both very much. I have 20 key-value-pairs, the values ​​are constant, no more are added and none are removed or changed.
                        @Christian-Ehrlicher do you think, that 20 pairs are too much so that the QMap would be slow?

                        P Offline
                        P Offline
                        Pl45m4
                        wrote on 26 Mar 2020, 15:38 last edited by
                        #12

                        @SpaceToon

                        20 list, map or array elements are nothing if you iterate correctly.

                        Here are several ways to iterate through a QMap
                        https://doc.qt.io/qt-5/qmap.html#details


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        1 Reply Last reply
                        1
                        • S SGaist
                          26 Mar 2020, 12:25

                          Hi,

                          @SpaceToon said in List of key-value pairs that can return both keys and values:

                          Thank you both very much. I have 20 key-value-pairs, the values ​​are constant, no more are added and none are removed or changed.
                          @Christian-Ehrlicher do you think, that 20 pairs are too much so that the QMap would be slow?

                          If you are having speed issue with 20 elements then it's likely somewhere else.

                          What makes you think that a slowdown may happen ?

                          S Offline
                          S Offline
                          SpaceToon
                          wrote on 26 Mar 2020, 16:47 last edited by
                          #13

                          @SGaist Hey, because in the doc is written (for const Key QMap::key(const T &value, const Key &defaultKey = Key()) const):

                          This function can be slow (linear time), because QMap's internal data structure is optimized for fast lookup by key, not by value.

                          Therefore I thought there could be performance issues. But I tested it and I did not recognize any performance disadvantages.

                          20 list, map or array elements are nothing if you iterate correctly.

                          Here are several ways to iterate through a QMap

                          https://doc.qt.io/qt-5/qmap.html#details

                          Thank you too!

                          J 1 Reply Last reply 26 Mar 2020, 16:53
                          0
                          • S SpaceToon
                            26 Mar 2020, 16:47

                            @SGaist Hey, because in the doc is written (for const Key QMap::key(const T &value, const Key &defaultKey = Key()) const):

                            This function can be slow (linear time), because QMap's internal data structure is optimized for fast lookup by key, not by value.

                            Therefore I thought there could be performance issues. But I tested it and I did not recognize any performance disadvantages.

                            20 list, map or array elements are nothing if you iterate correctly.

                            Here are several ways to iterate through a QMap

                            https://doc.qt.io/qt-5/qmap.html#details

                            Thank you too!

                            J Offline
                            J Offline
                            JonB
                            wrote on 26 Mar 2020, 16:53 last edited by
                            #14

                            @SpaceToon I might worry at 20,000, but not at 20 :)

                            1 Reply Last reply
                            2

                            11/14

                            26 Mar 2020, 12:25

                            • Login

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