Solved shuffling QStringList elements!
-
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?
-
@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"]
-
@joeQ No change the positions of all elements randomly!
-
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); }
-
@joeQ This is only for 2 elements right? I need for all the elements :)
-