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. How to remove elements from QStringList by comparing each other members.
Forum Update on Monday, May 27th 2025

How to remove elements from QStringList by comparing each other members.

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 6 Posters 1.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
    Ayush Gupta
    wrote on last edited by
    #1

    I have one QStringList code which are having some file names.

    I need to compare each file content and erase the file name if it has having indentical content. I need to delete the file name which are having indentical content and file name is greater in ascending order.

    Suppose there are 5 files with name
    Test1
    Test2
    Test3
    Test4
    Test5

    first i will compare the file contents of Test 1 and Test 2 and if content matches the delete Test 1 from the list and the compare contenst of Test 2 and Test 3 and if contents not matches than do +1 to the iteration and compare the contents of Test3 and Test4 and if contents matches then remove Test3 and then comapare contents Test2 and Test4..

    How can I do with the help of iterators. I am very afraid of invalidating the iterators using erase.

    1 Reply Last reply
    0
    • VRoninV VRonin

      Something like this?

      // QVector<QFile> fileList;
      // bool equalFiles(const QFile&,const QFile&);
      bool foundI;
      for(auto i=fileList.begin(), maxJ = fileList.end(), maxI=maxJ-1;i!=maxI;){
          foundI=false;
          for(auto j=i+1;j!=maxJ && !foundI;){
              if(equalFiles(*i,*j)){
                  if(i->fileName()>j->fileName())
                      j = fileList.erase(j);
                  else
                      foundI=true;
              }
              else
                  ++j;
          }
          if(foundI)
              i=fileList.erase(i);
          else
              ++i;
      }
      
      
      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #12

      @VRonin That's a brute force N! complexity algorithm on file contents. That's horrible.

      @Ayush-Gupta Create a [hash]->[filename] map. Read each file in sorted order and calculate their hash (MD5, SHA-1 or whatever works for you). Put the file name in the map using that hash and your resulting map will contain all the filenames with unique contents.

      Christian EhrlicherC 1 Reply Last reply
      4
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #2

        What if files Test2 and Test5 are identical and the others are unique?
        do you want to compare just adiecent files in the list?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Ayush Gupta
          wrote on last edited by
          #3

          I need to compare each file in list with another and remove the file if file has same contents and doing string comparison the name of the file which is less should be deleted.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Ayush Gupta
            wrote on last edited by
            #4

            @VRonin Can you help regardin this?

            mrjjM 1 Reply Last reply
            0
            • A Ayush Gupta

              @VRonin Can you help regardin this?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #5

              @Ayush-Gupta

              Hi
              How big are each file ?
              Can they all fit in memory at same time?

              What is the data inside ?

              Often one can use a checksum
              to see if files are the same but it depends on the data as
              sometimes a sequence of letters can have the same
              checksum as another even we logically would say they are not the same.

              1 Reply Last reply
              1
              • A Offline
                A Offline
                Ayush Gupta
                wrote on last edited by
                #6

                @mrjj
                I have the code to check if file are same or not.
                My problem is I am able to filter name from QStringList. I tried index also.
                Suppose there are 6 files in list and I need to compare each file ( for that I have code) which will do the thing.
                Then I need to compare the file name if fileName1 > fileName2 then delete fileName2 from list if comparison is equal.

                jsulmJ 1 Reply Last reply
                0
                • A Ayush Gupta

                  @mrjj
                  I have the code to check if file are same or not.
                  My problem is I am able to filter name from QStringList. I tried index also.
                  Suppose there are 6 files in list and I need to compare each file ( for that I have code) which will do the thing.
                  Then I need to compare the file name if fileName1 > fileName2 then delete fileName2 from list if comparison is equal.

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

                  @Ayush-Gupta https://doc.qt.io/qt-5/qlist.html#erase

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

                  1 Reply Last reply
                  1
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    Hi
                    so if
                    fileName1 and fileName2
                    is the same.
                    You must delete the file with the highest number in its name ?

                    A 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      Hi
                      so if
                      fileName1 and fileName2
                      is the same.
                      You must delete the file with the highest number in its name ?

                      A Offline
                      A Offline
                      Ayush Gupta
                      wrote on last edited by
                      #9

                      @mrjj yes

                      mrjjM 1 Reply Last reply
                      0
                      • A Ayush Gupta

                        @mrjj yes

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @Ayush-Gupta

                        Do you only have files 1 to max 9?

                            QString filename1 = "somename1";
                            QString filename2 = "somename2";
                        
                             int fno1=filename1.right(1).toInt();
                             int fno2=filename2.right(1).toInt();
                             
                             if (fno1 < fno2 ) ....
                        
                        

                        else you need better extraction.

                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by VRonin
                          #11

                          Something like this?

                          // QVector<QFile> fileList;
                          // bool equalFiles(const QFile&,const QFile&);
                          bool foundI;
                          for(auto i=fileList.begin(), maxJ = fileList.end(), maxI=maxJ-1;i!=maxI;){
                              foundI=false;
                              for(auto j=i+1;j!=maxJ && !foundI;){
                                  if(equalFiles(*i,*j)){
                                      if(i->fileName()>j->fileName())
                                          j = fileList.erase(j);
                                      else
                                          foundI=true;
                                  }
                                  else
                                      ++j;
                              }
                              if(foundI)
                                  i=fileList.erase(i);
                              else
                                  ++i;
                          }
                          
                          

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          Chris KawaC 1 Reply Last reply
                          0
                          • VRoninV VRonin

                            Something like this?

                            // QVector<QFile> fileList;
                            // bool equalFiles(const QFile&,const QFile&);
                            bool foundI;
                            for(auto i=fileList.begin(), maxJ = fileList.end(), maxI=maxJ-1;i!=maxI;){
                                foundI=false;
                                for(auto j=i+1;j!=maxJ && !foundI;){
                                    if(equalFiles(*i,*j)){
                                        if(i->fileName()>j->fileName())
                                            j = fileList.erase(j);
                                        else
                                            foundI=true;
                                    }
                                    else
                                        ++j;
                                }
                                if(foundI)
                                    i=fileList.erase(i);
                                else
                                    ++i;
                            }
                            
                            
                            Chris KawaC Offline
                            Chris KawaC Offline
                            Chris Kawa
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @VRonin That's a brute force N! complexity algorithm on file contents. That's horrible.

                            @Ayush-Gupta Create a [hash]->[filename] map. Read each file in sorted order and calculate their hash (MD5, SHA-1 or whatever works for you). Put the file name in the map using that hash and your resulting map will contain all the filenames with unique contents.

                            Christian EhrlicherC 1 Reply Last reply
                            4
                            • Chris KawaC Chris Kawa

                              @VRonin That's a brute force N! complexity algorithm on file contents. That's horrible.

                              @Ayush-Gupta Create a [hash]->[filename] map. Read each file in sorted order and calculate their hash (MD5, SHA-1 or whatever works for you). Put the file name in the map using that hash and your resulting map will contain all the filenames with unique contents.

                              Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              @Chris-Kawa Before creating the hash I would compare the filesize (and hope that they all differ :) )

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

                              Chris KawaC 1 Reply Last reply
                              5
                              • Christian EhrlicherC Christian Ehrlicher

                                @Chris-Kawa Before creating the hash I would compare the filesize (and hope that they all differ :) )

                                Chris KawaC Offline
                                Chris KawaC Offline
                                Chris Kawa
                                Lifetime Qt Champion
                                wrote on last edited by
                                #14

                                @Christian-Ehrlicher Good idea. You could use the size as initial hash value and only do a "full" hash for hash conflict resolution (caching the result of course).

                                1 Reply Last reply
                                3
                                • A Offline
                                  A Offline
                                  Ayush Gupta
                                  wrote on last edited by
                                  #15

                                  @Chris-Kawa Thanks

                                  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