📄 ran_2368.htm
字号:
<HTML><TITLE>random_shuffle </TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>random_shuffle </H2>
<HR><PRE> Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Randomly shuffles elements of a collection.</P>
<PRE></PRE>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Complexity"><LI>Complexity</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warning"><LI>Warning</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <algorithm></PRE>
<PRE>
template <class RandomAccessIterator>
void <B>random_shuffle</B> (RandomAccessIterator first,
RandomAccessIterator last);
template <class RandomAccessIterator,
class RandomNumberGenerator>
void <B>random_shuffle</B> (RandomAccessIterator first,
RandomAccessIterator last,
RandomNumberGenerator& rand);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>random_shuffle</B></I> algorithm shuffles the elements in the range <SAMP>[first, last)</SAMP> with uniform distribution. <B><I>random_shuffle</B></I> can take a particular random number generating function object <SAMP>rand</SAMP> , where <SAMP>rand</SAMP> takes a positive argument <SAMP>n</SAMP> of distance <SAMP>type</SAMP> of the <SAMP>RandomAccessIterator</SAMP> and returns a randomly chosen value between <SAMP>0 </SAMP>and<SAMP> n - 1</SAMP>. </P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P>In the <B><I>random_shuffle</B></I> algorithm, <SAMP>(last - first) -1 </SAMP>swaps are done.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// rndshufl.cpp
//
#include <algorithm>
#include <vector>
#include <iostream.h></PRE><PRE> int main()
{
//Initialize a vector with an array of ints
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
vector<int> v(arr, arr+10);</PRE><PRE> //Print out elements in original (sorted) order
cout << "Elements before random_shuffle: " << endl << " ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;</PRE><PRE> //Mix them up with random_shuffle
<B>random_shuffle</B>(v.begin(), v.end());</PRE>
<PRE> //Print out the mixed up elements
cout << "Elements after random_shuffle: " << endl << " ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl;
return 0;
}
Output :
Elements before random_shuffle:
1 2 3 4 5 6 7 8 9 10
Elements after random_shuffle:
7 9 10 3 2 5 4 8 1 6</PRE>
<A NAME="Warning"><H3>Warning</H3></A>
<P>If your compiler does not support default template parameters, you need to always supply the <SAMP>Allocator</SAMP> template argument. For instance, you will need to write :</P>
<P><SAMP>vector<int, allocator></SAMP></P>
<P>instead of :</P>
<P><SAMP>vector<int></SAMP></P>
<HR>
<A HREF="Ran_7047.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="raw_6773.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -