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. Is it possible to have duplicate in QSet/Hash ?
Forum Updated to NodeBB v4.3 + New Features

Is it possible to have duplicate in QSet/Hash ?

Scheduled Pinned Locked Moved Solved General and Desktop
qsetqhashduplicate
7 Posts 2 Posters 217 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.
  • A Offline
    A Offline
    AntGP
    wrote last edited by aha_1980
    #1

    Hello,

    I just wanted to ask a simple question to confirm what I'm trying to understand.

    From what I have understood so far, Qset/Hash documentation doesn't say anything about the potentiality of a duplicate, but the way they are made, if a data is inserted in a Qset on which the data already exists, it will not create a second entry on the QSet ? Otherwise, with the arbitrary order of data storage, it would be impossible to differentiate them.

    So my question based on this understanding is : If I don't want duplicates (compared to the case of a QList), is it always true that if I have two exact entries I have no duplicates ?

    Exemple :

    
    QSet<QString> set;
    
    set.insert("one");
    
    set.insert("three");
    
    set.insert("one");
    
    

    In this case, inspried by the one of the documentation, I will only have "one" and "three" stored ?

    And will be the case for every type of data, even in the case of a Qset<QListe<QString>> ? (the one I actually want to use)

    I did not found where this propriety even if logic is stated, if it exist at all.

    Pl45m4P 2 Replies Last reply
    0
    • A AntGP

      Hello,

      I just wanted to ask a simple question to confirm what I'm trying to understand.

      From what I have understood so far, Qset/Hash documentation doesn't say anything about the potentiality of a duplicate, but the way they are made, if a data is inserted in a Qset on which the data already exists, it will not create a second entry on the QSet ? Otherwise, with the arbitrary order of data storage, it would be impossible to differentiate them.

      So my question based on this understanding is : If I don't want duplicates (compared to the case of a QList), is it always true that if I have two exact entries I have no duplicates ?

      Exemple :

      
      QSet<QString> set;
      
      set.insert("one");
      
      set.insert("three");
      
      set.insert("one");
      
      

      In this case, inspried by the one of the documentation, I will only have "one" and "three" stored ?

      And will be the case for every type of data, even in the case of a Qset<QListe<QString>> ? (the one I actually want to use)

      I did not found where this propriety even if logic is stated, if it exist at all.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote last edited by Pl45m4
      #2

      @AntGP said in Is it possible to have duplicate in QSet/Harsh ?:

      Qset/Harsh documentation doesn't say anything about the potentiality of a duplicate, but the way they are made, if a data is inserted in a Qset on which the data already exists, it will not create a second entry on the QSet ?

      At least for QHash it does...

      QHash is unordered, so an iterator's sequence cannot be assumed to be predictable. If ordering by key is required, use a QMap.

      Normally, a QHash allows only one value per key. If you call insert() with a key that already exists in the QHash, the previous value is erased. For example:

      hash.insert("plenty", 100);
      
      hash.insert("plenty", 2000);
      
      // hash.value("plenty") == 2000
      

      (https://doc.qt.io/qt-6/qhash.html)

      That also includes assigning the same value again... which means the QHash pair is updated (to the same value but that does not matter here)... So no multiple values for the same hashed key.

      For this there is QMultiHash, which allows multiple values per key.

      Edit:

      Because QSet has an internal QHash table but no key, I think the same applies.


      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
      4
      • A Offline
        A Offline
        AntGP
        wrote last edited by
        #3

        Thx ! it answered my question perfectly !

        1 Reply Last reply
        0
        • A AntGP has marked this topic as solved
        • A AntGP

          Hello,

          I just wanted to ask a simple question to confirm what I'm trying to understand.

          From what I have understood so far, Qset/Hash documentation doesn't say anything about the potentiality of a duplicate, but the way they are made, if a data is inserted in a Qset on which the data already exists, it will not create a second entry on the QSet ? Otherwise, with the arbitrary order of data storage, it would be impossible to differentiate them.

          So my question based on this understanding is : If I don't want duplicates (compared to the case of a QList), is it always true that if I have two exact entries I have no duplicates ?

          Exemple :

          
          QSet<QString> set;
          
          set.insert("one");
          
          set.insert("three");
          
          set.insert("one");
          
          

          In this case, inspried by the one of the documentation, I will only have "one" and "three" stored ?

          And will be the case for every type of data, even in the case of a Qset<QListe<QString>> ? (the one I actually want to use)

          I did not found where this propriety even if logic is stated, if it exist at all.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote last edited by
          #4

          @AntGP said in Is it possible to have duplicate in QSet/Harsh ?:

          Thx ! it answered my question perfectly !

          Great!

          @AntGP said in Is it possible to have duplicate in QSet/Harsh ?:

          even in the case of a Qset<QListe<QString>> ? (the one I actually want to use)

          For convenience use QSet<QStringList> instead, which is exactly a QList of QStrings plus some extra features


          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
          • A Offline
            A Offline
            AntGP
            wrote last edited by AntGP
            #5

            It would have been a good suggestion , but.... i'm not the one who made the Qlist<Qstring>... i'm trying to look at something 12 years old, made over 10 years by at least 3 differents persons whit no doc

            Pl45m4P 1 Reply Last reply
            0
            • A AntGP

              It would have been a good suggestion , but.... i'm not the one who made the Qlist<Qstring>... i'm trying to look at something 12 years old, made over 10 years by at least 3 differents persons whit no doc

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote last edited by Pl45m4
              #6

              @AntGP said in Is it possible to have duplicate in QSet/Harsh ?:

              i'm not the one who made the Qlist<Qstring>

              That should not matter?!
              But you are trying to put that QList<QString> construct in a container of your choice (QHash / QMap / QSet) right?
              So you can still make it QSet<QStringList> and add the old data to it, in fact QStringList IS a QList<QString>, with its own API on top.

              • https://doc.qt.io/qt-6/qstringlist.html#details

              Of course you don't have to... but sometimes things like

              QStringList() << "Foo" << "Bar" << "42" << "Mooh"; 
              

              as well as joins and filters, come quite handy :)


              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
              0
              • A Offline
                A Offline
                AntGP
                wrote last edited by AntGP
                #7

                To be honnest, looking more deeply in Qhash than i'v done before, it appered that it was the perfect tool for a bit later.
                Once my thing is working and I have fixed my own mess, I'll take a look at improving what mess have been made before me.

                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