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. QStringLIst get duplicates ?
Forum Updated to NodeBB v4.3 + New Features

QStringLIst get duplicates ?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 6 Posters 3.3k Views 1 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    First, I need to chop(1) every QString in QStringList.
    Second, get duplicate 2, 3 ,4 times QString .

    https://github.com/sonichy

    aha_1980A J.HilkJ 2 Replies Last reply
    0
    • sonichyS sonichy

      First, I need to chop(1) every QString in QStringList.
      Second, get duplicate 2, 3 ,4 times QString .

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @sonichy

      So like here: https://stackoverflow.com/questions/40999746/looking-inside-a-qstringlist-for-duplicate-strings-and-getting-a-count-of-duplic ?

      Qt has to stay free or it will die.

      sonichyS 1 Reply Last reply
      1
      • aha_1980A aha_1980

        @sonichy

        So like here: https://stackoverflow.com/questions/40999746/looking-inside-a-qstringlist-for-duplicate-strings-and-getting-a-count-of-duplic ?

        sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by sonichy
        #3

        @aha_1980 Miraculous!

        QMap<QString,int> countOfStrings;
        QStringList listOfStrings;
        listOfStrings << "a" << "b" << "c" << "a";
        for(int i=0; i<listOfStrings.count(); i++)
        {
            countOfStrings[listOfStrings[i]]++;
        }
        qDebug() << countOfStrings;
        

        But how to read 2 times item out ?

        https://github.com/sonichy

        jsulmJ 1 Reply Last reply
        0
        • sonichyS sonichy

          @aha_1980 Miraculous!

          QMap<QString,int> countOfStrings;
          QStringList listOfStrings;
          listOfStrings << "a" << "b" << "c" << "a";
          for(int i=0; i<listOfStrings.count(); i++)
          {
              countOfStrings[listOfStrings[i]]++;
          }
          qDebug() << countOfStrings;
          

          But how to read 2 times item out ?

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

          @sonichy said in QStringLIst get duplicates ?:

          But how to read 2 times item out ?

          What do you mean?

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

          1 Reply Last reply
          3
          • sonichyS sonichy

            First, I need to chop(1) every QString in QStringList.
            Second, get duplicate 2, 3 ,4 times QString .

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            Hi @sonichy I'm at a loss what you want to do in the first place, what does:

            I need to chop(1) every QString in QStringList.

            mean?

            Do you have a QStringList and you want to reduce all QStrings in that list down to 1 character only?

                QStringList myStringList({"abc", "bca", "aaced", "fgh"});
                for( QString &str: myStringList)
                    if(!str.isEmpty())
                         str = str.at(0);
            //will result in (a, b,a,f);
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            JonBJ 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              Hi @sonichy I'm at a loss what you want to do in the first place, what does:

              I need to chop(1) every QString in QStringList.

              mean?

              Do you have a QStringList and you want to reduce all QStrings in that list down to 1 character only?

                  QStringList myStringList({"abc", "bca", "aaced", "fgh"});
                  for( QString &str: myStringList)
                      if(!str.isEmpty())
                           str = str.at(0);
              //will result in (a, b,a,f);
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @J.Hilk , @sonichy
              At the risk of guessing the OP's intentions, I have a feeling he actually means "how does he find (and remove, or not count?) any duplicates in the QStringList"? Hmm, maybe not quite, since he's already counting that in his QMap. Maybe he wants to output each string the number of times in the count for that string. Maybe I'm talking nonsense and it's to do with the "chop(1)". I love this guessing game.... :)

              J.HilkJ 1 Reply Last reply
              1
              • JonBJ JonB

                @J.Hilk , @sonichy
                At the risk of guessing the OP's intentions, I have a feeling he actually means "how does he find (and remove, or not count?) any duplicates in the QStringList"? Hmm, maybe not quite, since he's already counting that in his QMap. Maybe he wants to output each string the number of times in the count for that string. Maybe I'm talking nonsense and it's to do with the "chop(1)". I love this guessing game.... :)

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #7

                @JonB
                well QStringList is a complex tool and has for nearly everything a build in function:

                There's removeDuplicates, to remove Duplicates ;-)
                There's count(const T &), that returns how often the QString is in the list
                and there's indexOf(const T &, int ) that returns the char index of the first appearence of the QString from the specified int-index on forward


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                sonichyS 1 Reply Last reply
                5
                • J.HilkJ J.Hilk

                  @JonB
                  well QStringList is a complex tool and has for nearly everything a build in function:

                  There's removeDuplicates, to remove Duplicates ;-)
                  There's count(const T &), that returns how often the QString is in the list
                  and there's indexOf(const T &, int ) that returns the char index of the first appearence of the QString from the specified int-index on forward

                  sonichyS Offline
                  sonichyS Offline
                  sonichy
                  wrote on last edited by
                  #8

                  @J.Hilk Count(T) is easier and basic than QMap !

                  https://github.com/sonichy

                  JKSHJ 1 Reply Last reply
                  0
                  • sonichyS sonichy

                    @J.Hilk Count(T) is easier and basic than QMap !

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    @sonichy said in QStringLIst get duplicates ?:

                    @J.Hilk Count(T) is easier and basic than QMap !

                    Yes, it is easier.

                    It is also less efficient if you count many different strings.

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    1

                    • Login

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