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 Updated to NodeBB v4.3 + New Features

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