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

Kermit Mei kermit.mei at gmail.com
Fri Mar 12 07:14:57 CET 2010


On Fri, 2010-03-12 at 07:07 +0100, Liang Qi wrote:
> 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
> 

Hmm,I had known this method in stl before, but I compiled Qt with
-no-stl, so I think I can't use it. It looks like that I have to
reimplement that by hand:)

Thanks
B.R
Kermit Mei

> 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