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.
  • S SpaceToon

    @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))];
    
    Pablo J. RoginaP Offline
    Pablo J. RoginaP Offline
    Pablo J. Rogina
    wrote on 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
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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

      Pablo J. RoginaP 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @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.

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on 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

        Christian EhrlicherC 1 Reply Last reply
        0
        • Pablo J. RoginaP Pablo J. Rogina

          @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.

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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

          Pablo J. RoginaP 1 Reply Last reply
          6
          • Christian EhrlicherC Christian Ehrlicher

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

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on 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 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?

              SGaistS Pl45m4P 2 Replies Last reply
              0
              • S SpaceToon

                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?

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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
                3
                • S SpaceToon

                  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?

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on 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
                  • SGaistS SGaist

                    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 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!

                    JonBJ 1 Reply Last reply
                    0
                    • S SpaceToon

                      @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!

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #14

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

                      1 Reply Last reply
                      2

                      • Login

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