[Qt-interest] [qt-china] Is there a shuffle algorithm in Qt?

Liang Qi cavendish.qi at gmail.com
Fri Mar 12 07:07:11 CET 2010


Write your own? You could have a look at the code of random_shuffle:
http://www.cplusplus.com/reference/algorithm/random_shuffle/
template <class RandomAccessIterator, class RandomNumberGenerator>

void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last,
                        RandomNumberGenerator& rand )
{
  iterator_traits<RandomAccessIterator>::difference_type i, n;
  n = (last-first);
  for (i=2; i<n; ++i) swap (first[i],first[rand(i)]);
}

And you have qSwap and qrand/qsrand:
http://doc.trolltech.com/4.6/qtalgorithms.html#qSwap
http://doc.trolltech.com/4.6/qtglobal.html#qrand

Regards,
Liang

2010/3/12 Kermit Mei <kermit.mei at gmail.com>:
> Dear all,
>    I want to shuffle a QVector or QList container, is there an
> available algorithm to do it? I can't find that in QtAlgorithm or the
> containers' assistant.
>    By the way, I can't use stl-algorithm in my embedded environment.
>
> Thanks
> B.R
> Kermit Mei
>

-- 
http://www.qiliang.net




More information about the Qt-interest-old mailing list