⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ran_2368.htm

📁 ARM编辑、编译软件
💻 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>&copy;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 &#60;algorithm></PRE>
<PRE>
template &#60;class RandomAccessIterator>
 void <B>random_shuffle</B> (RandomAccessIterator first,
                      RandomAccessIterator last);
template &#60;class RandomAccessIterator,
          class RandomNumberGenerator>
 void <B>random_shuffle</B> (RandomAccessIterator first,
                      RandomAccessIterator last,
                      RandomNumberGenerator&#38; 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 &#60;algorithm>
 #include &#60;vector>
 #include &#60;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&#60;int> v(arr, arr+10);</PRE><PRE>   //Print out elements in original (sorted) order
   cout &#60;&#60; "Elements before random_shuffle: " &#60;&#60; endl &#60;&#60; "     ";
   copy(v.begin(),v.end(),ostream_iterator&#60;int>(cout," "));
   cout &#60;&#60; endl &#60;&#60; 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 &#60;&#60; "Elements after random_shuffle: " &#60;&#60; endl &#60;&#60; "     ";
   copy(v.begin(),v.end(),ostream_iterator&#60;int>(cout," "));
   cout &#60;&#60; 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&#60;int, allocator></SAMP></P>
<P>instead of :</P>
<P><SAMP>vector&#60;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 + -