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. QHash, how to update value in map?
Forum Updated to NodeBB v4.3 + New Features

QHash, how to update value in map?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 6 Posters 1.5k 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.
  • O Offline
    O Offline
    ollarch
    wrote on last edited by
    #2

    Hi,

    Use [] operator.

    SPlattenS 1 Reply Last reply
    1
    • O ollarch

      Hi,

      Use [] operator.

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by SPlatten
      #3

      @ollarch , can you show a demonstration please?

      Tried:

      mhmTrainees[itFound] = uint64Value;
      

      Compile errors:

      C2679: binary '[': no operator found which takes a right-hand of type 'QHash<QString, quint64>::iterator'
      

      Edit...

      mhmTrainees[itFound.key()] = uint64Value;
      

      This works!

      Kind Regards,
      Sy

      JonBJ 1 Reply Last reply
      0
      • SPlattenS SPlatten

        @ollarch , can you show a demonstration please?

        Tried:

        mhmTrainees[itFound] = uint64Value;
        

        Compile errors:

        C2679: binary '[': no operator found which takes a right-hand of type 'QHash<QString, quint64>::iterator'
        

        Edit...

        mhmTrainees[itFound.key()] = uint64Value;
        

        This works!

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #4

        @SPlatten
        I'm not a wiz with iterators, but you should be able to do it from the right iterator (without having to re-look-up the key). See https://doc.qt.io/qt-5/qhash-iterator.html#details

        Let's see a few examples of things we can do with a QHash::iterator that we cannot do with a QHash::const_iterator. Here's an example that increments every value stored in the QHash by 2:

        QHash<QString, int>::iterator i;
        for (i = hash.begin(); i != hash.end(); ++i)
            i.value() += 2;
        
        SPlattenS 1 Reply Last reply
        3
        • JonBJ JonB

          @SPlatten
          I'm not a wiz with iterators, but you should be able to do it from the right iterator (without having to re-look-up the key). See https://doc.qt.io/qt-5/qhash-iterator.html#details

          Let's see a few examples of things we can do with a QHash::iterator that we cannot do with a QHash::const_iterator. Here's an example that increments every value stored in the QHash by 2:

          QHash<QString, int>::iterator i;
          for (i = hash.begin(); i != hash.end(); ++i)
              i.value() += 2;
          
          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #5

          @JonB , the problem is that the iterator itself is not a pointer or a reference so there is no link to the original data source it references, I've resolved this now.

          Kind Regards,
          Sy

          KroMignonK 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @JonB , the problem is that the iterator itself is not a pointer or a reference so there is no link to the original data source it references, I've resolved this now.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #6

            @SPlatten said in QHash, how to update value in map?:

            the problem is that the iterator itself is not a pointer or a reference so there is no link to the original data source it references, I've resolved this now.

            the iterator holds a reference to the item (key and value)!

            The code given by @JonB is the right way to do (cf. https://doc.qt.io/qt-5/qhash-iterator.html#details)
            mhmTrainees[itFound.key()] = uint64Value; should be itFound.value() = uint64Value;

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            SPlattenS 1 Reply Last reply
            1
            • KroMignonK KroMignon

              @SPlatten said in QHash, how to update value in map?:

              the problem is that the iterator itself is not a pointer or a reference so there is no link to the original data source it references, I've resolved this now.

              the iterator holds a reference to the item (key and value)!

              The code given by @JonB is the right way to do (cf. https://doc.qt.io/qt-5/qhash-iterator.html#details)
              mhmTrainees[itFound.key()] = uint64Value; should be itFound.value() = uint64Value;

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #7

              @KroMignon , that was exactly what my original code looked like and it doesn't work, I can see whilst the iterator is modified the original value in the QHash isn't modified and I'm not surprised because .begin() doesn't return a reference it just returns an iterator so there is no link.

              Kind Regards,
              Sy

              jsulmJ 1 Reply Last reply
              0
              • SPlattenS SPlatten

                @KroMignon , that was exactly what my original code looked like and it doesn't work, I can see whilst the iterator is modified the original value in the QHash isn't modified and I'm not surprised because .begin() doesn't return a reference it just returns an iterator so there is no link.

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

                @SPlatten said in QHash, how to update value in map?:

                begin() doesn't return a reference it just returns an iterator so there is no link

                As already said by @KroMignon the iterator holds the reference...
                It is also shown in https://doc.qt.io/qt-5/qhash-iterator.html

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

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  mpergand
                  wrote on last edited by mpergand
                  #9

                  Sorry @SPlatten but it works as @JonB said

                      typedef QHash<QString,int> H;
                      H h;
                      h["A"]=1;
                      h["B"]=2;
                      
                      for( auto& i: h)
                          {
                          i++;
                          }
                      
                      qDebug()<<h;
                      
                      QHash<QString, int>::iterator i;
                      for (i = h.begin(); i != h.end(); ++i)
                          {
                          i.value()++;
                          }
                      
                      qDebug()<<h;
                  

                  Logs:
                  QHash(("B", 3)("A", 2))
                  QHash(("B", 4)("A", 3))

                  SPlattenS 1 Reply Last reply
                  3
                  • M mpergand

                    Sorry @SPlatten but it works as @JonB said

                        typedef QHash<QString,int> H;
                        H h;
                        h["A"]=1;
                        h["B"]=2;
                        
                        for( auto& i: h)
                            {
                            i++;
                            }
                        
                        qDebug()<<h;
                        
                        QHash<QString, int>::iterator i;
                        for (i = h.begin(); i != h.end(); ++i)
                            {
                            i.value()++;
                            }
                        
                        qDebug()<<h;
                    

                    Logs:
                    QHash(("B", 3)("A", 2))
                    QHash(("B", 4)("A", 3))

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #10

                    @mpergand , don't apologise, this isn't what I am seeing in Qt Creator / Debugger.

                    Kind Regards,
                    Sy

                    jsulmJ KroMignonK 2 Replies Last reply
                    0
                    • SPlattenS SPlatten

                      @mpergand , don't apologise, this isn't what I am seeing in Qt Creator / Debugger.

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

                      @SPlatten Then you are doing something wrong because I just tried the code from @mpergand and it works as expected...

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

                      SPlattenS 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @SPlatten Then you are doing something wrong because I just tried the code from @mpergand and it works as expected...

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #12

                        @jsulm , ok, just tried it again and of course it is working as you say, not sure how or how I got it so wrong...

                        Kind Regards,
                        Sy

                        1 Reply Last reply
                        0
                        • SPlattenS SPlatten

                          @mpergand , don't apologise, this isn't what I am seeing in Qt Creator / Debugger.

                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by KroMignon
                          #13

                          @SPlatten said in QHash, how to update value in map?:

                          don't apologise, this isn't what I am seeing in Qt Creator / Debugger.

                          I am very surprised!
                          I always do it like this (it.value() = newValue) and it is the way it is documented.
                          Can you show how you have set the iterator?
                          It is an iterator or const_iterator?

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          1 Reply Last reply
                          0

                          • Login

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