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. shuffling QStringList elements!
Forum Updated to NodeBB v4.3 + New Features

shuffling QStringList elements!

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 4.0k 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.
  • K Offline
    K Offline
    Kushan
    wrote on last edited by
    #1

    In my Qt c++ application I have a QStringList containing a set of QString values! I want to shuffle(change the positions of the QStrings in the QStringList arbitrarily) . Is there any default function like "shuffle_array" API in perl? If not how can I do it?

    eg-
    QStringList names;
    names<<"John"<<"Smith"<<"Anne";

    shuffling may change the positions of John,Smith and Anne arbitrarily! How can I achieve this?

    joeQJ 1 Reply Last reply
    0
    • K Kushan

      In my Qt c++ application I have a QStringList containing a set of QString values! I want to shuffle(change the positions of the QStrings in the QStringList arbitrarily) . Is there any default function like "shuffle_array" API in perl? If not how can I do it?

      eg-
      QStringList names;
      names<<"John"<<"Smith"<<"Anne";

      shuffling may change the positions of John,Smith and Anne arbitrarily! How can I achieve this?

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #2

      @Kushan Hi, friend. Welcome.

      Did your mean want to swap two element position?

      if Yes. you can use void QList::swap(int i,intj)

      Just like Qt help manual like:

        QList<QString> list;
        list << "A" << "B" << "C" << "D" << "E" << "F";
        list.swap(1, 4);
        // list: ["A", "E", "C", "D", "B", "F"]
      

      Just do it!

      K 1 Reply Last reply
      1
      • joeQJ joeQ

        @Kushan Hi, friend. Welcome.

        Did your mean want to swap two element position?

        if Yes. you can use void QList::swap(int i,intj)

        Just like Qt help manual like:

          QList<QString> list;
          list << "A" << "B" << "C" << "D" << "E" << "F";
          list.swap(1, 4);
          // list: ["A", "E", "C", "D", "B", "F"]
        
        K Offline
        K Offline
        Kushan
        wrote on last edited by
        #3

        @joeQ No change the positions of all elements randomly!

        joeQJ 1 Reply Last reply
        0
        • K Kushan

          @joeQ No change the positions of all elements randomly!

          joeQJ Offline
          joeQJ Offline
          joeQ
          wrote on last edited by joeQ
          #4

          @Kushan

          Ok, like below ?

          void getTwoSwapPosition(int min,int max,int &a,int &b)
          {
              /// you could use qrand function get two random number as a,b
              /// a and b must between in min and max [min,max]
          }
          
          int a,b,min,max;
          min = 0;
          max = strlist.size()-1;
          for(/**....*/){
             getTwoSwapPosition(min,max,a,b);
             strlist.swap(a,b);
          }

          Just do it!

          K 1 Reply Last reply
          1
          • joeQJ joeQ

            @Kushan

            Ok, like below ?

            void getTwoSwapPosition(int min,int max,int &a,int &b)
            {
                /// you could use qrand function get two random number as a,b
                /// a and b must between in min and max [min,max]
            }
            
            int a,b,min,max;
            min = 0;
            max = strlist.size()-1;
            for(/**....*/){
               getTwoSwapPosition(min,max,a,b);
               strlist.swap(a,b);
            }
            K Offline
            K Offline
            Kushan
            wrote on last edited by
            #5

            @joeQ This is only for 2 elements right? I need for all the elements :)

            Taz742T 1 Reply Last reply
            0
            • K Kushan

              @joeQ This is only for 2 elements right? I need for all the elements :)

              Taz742T Offline
              Taz742T Offline
              Taz742
              wrote on last edited by
              #6

              @Kushan
              http://www.cplusplus.com/reference/algorithm/random_shuffle/

              Do what you want.

              1 Reply Last reply
              4

              • Login

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