PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/orig/stl/algorithm/next_permutation.html

http://github.com/kazuho/cppref
HTML | 216 lines | 163 code | 46 blank | 7 comment | 0 complexity | fd42a687d6cb858d62c0d3d613969f4e MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
  4. lang="en" dir="ltr">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <title>
  8. next_permutation [C++ Reference]
  9. </title>
  10. <meta name="generator" content="DokuWiki Release 2009-12-25c &quot;Lemming&quot;" />
  11. <meta name="robots" content="index,follow" />
  12. <meta name="date" content="2010-04-07T04:47:10-0700" />
  13. <meta name="keywords" content="stl,algorithm,next_permutation" />
  14. <link rel="search" type="application/opensearchdescription+xml" href="/wiki/lib/exe/opensearch.php" title="C++ Reference" />
  15. <link rel="start" href="/wiki/" />
  16. <link rel="contents" href="/wiki/stl/algorithm/next_permutation?do=index" title="Index" />
  17. <link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/wiki/feed.php" />
  18. <link rel="alternate" type="application/rss+xml" title="Current Namespace" href="/wiki/feed.php?mode=list&amp;ns=stl:algorithm" />
  19. <link rel="edit" title="Edit this page" href="/wiki/stl/algorithm/next_permutation?do=edit" />
  20. <link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/stl/algorithm/next_permutation" />
  21. <link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/stl/algorithm/next_permutation" />
  22. <link rel="canonical" href="http://www.cppreference.com/wiki/stl/algorithm/next_permutation" />
  23. <link rel="stylesheet" media="all" type="text/css" href="/wiki/lib/exe/css.php?s=all&amp;t=custom1&amp;tseed=1272971091" />
  24. <link rel="stylesheet" media="screen" type="text/css" href="/wiki/lib/exe/css.php?t=custom1&amp;tseed=1272971091" />
  25. <link rel="stylesheet" media="print" type="text/css" href="/wiki/lib/exe/css.php?s=print&amp;t=custom1&amp;tseed=1272971091" />
  26. <script type="text/javascript" charset="utf-8" ><!--//--><![CDATA[//><!--
  27. var NS='stl:algorithm';var JSINFO = {"id":"stl:algorithm:next_permutation","namespace":"stl:algorithm"};
  28. //--><!]]></script>
  29. <script type="text/javascript" charset="utf-8" src="/wiki/lib/exe/js.php?tseed=1272971091" ></script>
  30. <link rel="shortcut icon" href="/wiki/lib/tpl/custom1/images/favicon.png" />
  31. </head>
  32. <body>
  33. <div class="dokuwiki">
  34. <div class="stylehead">
  35. <div class="header">
  36. <div class="pagename">
  37. [[<a href="../../stl/algorithm/next_permutation.html" title="Backlinks">next_permutation</a>]]
  38. </div>
  39. <div class="logo">
  40. <a href="http://www.cppreference.com" name="dokuwiki__top" id="dokuwiki__top" accesskey="h" title="[ALT+H]">C++ Reference</a> </div>
  41. <div class="clearer"></div>
  42. </div>
  43. <div class="breadcrumbs">
  44. <span class="bchead">You are here: </span><a href="../../start.html" title="start">C++ Reference</a> &raquo; <a href="../../stl/start.html" title="stl:start">C++ Standard Template Library</a> &raquo; <a href="../../stl/algorithm/start.html" title="stl:algorithm:start">C++ Algorithms</a> &raquo; <a href="../../stl/algorithm/next_permutation.html" title="stl:algorithm:next_permutation">next_permutation</a> </div>
  45. </div>
  46. <div class="plugin_translation"><span>Translations of this page<sup><a href="../../localization.html" class="wikilink1" title="localization">?</a></sup>:</span> <ul> <li><div class="li"><span class="curid"><a href="../../stl/algorithm/next_permutation.html" class="wikilink1" title="stl:algorithm:next_permutation">en</a></span></div></li> <li><div class="li"><a href="../../br-pt/stl/algorithm/next_permutation.html" class="wikilink2" title="br-pt:stl:algorithm:next_permutation" rel="nofollow">br-pt</a></div></li> <li><div class="li"><a href="../../cn/stl/algorithm/next_permutation.html" class="wikilink2" title="cn:stl:algorithm:next_permutation" rel="nofollow">cn</a></div></li> <li><div class="li"><a href="../../cz/stl/algorithm/next_permutation.html" class="wikilink2" title="cz:stl:algorithm:next_permutation" rel="nofollow">cz</a></div></li> <li><div class="li"><a href="../../de/stl/algorithm/next_permutation.html" class="wikilink2" title="de:stl:algorithm:next_permutation" rel="nofollow">de</a></div></li> <li><div class="li"><a href="../../es/stl/algorithm/next_permutation.html" class="wikilink1" title="es:stl:algorithm:next_permutation">es</a></div></li> <li><div class="li"><a href="../../fr/stl/algorithm/next_permutation.html" class="wikilink2" title="fr:stl:algorithm:next_permutation" rel="nofollow">fr</a></div></li> <li><div class="li"><a href="../../it/stl/algorithm/next_permutation.html" class="wikilink2" title="it:stl:algorithm:next_permutation" rel="nofollow">it</a></div></li> <li><div class="li"><a href="../../jp/stl/algorithm/next_permutation.html" class="wikilink2" title="jp:stl:algorithm:next_permutation" rel="nofollow">jp</a></div></li> <li><div class="li"><a href="../../nl/stl/algorithm/next_permutation.html" class="wikilink2" title="nl:stl:algorithm:next_permutation" rel="nofollow">nl</a></div></li> <li><div class="li"><a href="../../pl/stl/algorithm/next_permutation.html" class="wikilink2" title="pl:stl:algorithm:next_permutation" rel="nofollow">pl</a></div></li> <li><div class="li"><a href="../../ro/stl/algorithm/next_permutation.html" class="wikilink2" title="ro:stl:algorithm:next_permutation" rel="nofollow">ro</a></div></li> <li><div class="li"><a href="../../ru/stl/algorithm/next_permutation.html" class="wikilink2" title="ru:stl:algorithm:next_permutation" rel="nofollow">ru</a></div></li> <li><div class="li"><a href="../../sk/stl/algorithm/next_permutation.html" class="wikilink2" title="sk:stl:algorithm:next_permutation" rel="nofollow">sk</a></div></li> <li><div class="li"><a href="../../tr/stl/algorithm/next_permutation.html" class="wikilink2" title="tr:stl:algorithm:next_permutation" rel="nofollow">tr</a></div></li> <li><div class="li"><a href="../../tw/stl/algorithm/next_permutation.html" class="wikilink2" title="tw:stl:algorithm:next_permutation" rel="nofollow">tw</a></div></li></ul></div>
  47. <div class="page">
  48. <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
  49. </script>
  50. <script type="text/javascript">
  51. _uacct = "UA-2828341-1";
  52. urchinTracker();
  53. </script>
  54. <!-- wikipage start -->
  55. <h2><a name="next_permutation" id="next_permutation">next_permutation</a></h2>
  56. <div class="level2">
  57. <p>
  58. Syntax:
  59. </p>
  60. <pre class="c code c++" style="font-family:monospace;"> <span class="co2">#include &lt;algorithm&gt;</span>
  61. bool next_permutation<span class="br0">&#40;</span> bidirectional_iterator start<span class="sy0">,</span> bidirectional_iterator end <span class="br0">&#41;</span><span class="sy0">;</span>
  62. bool next_permutation<span class="br0">&#40;</span> bidirectional_iterator start<span class="sy0">,</span> bidirectional_iterator end<span class="sy0">,</span> StrictWeakOrdering cmp <span class="br0">&#41;</span><span class="sy0">;</span></pre>
  63. <p>
  64. Transform range to next permutation
  65. </p>
  66. <p>
  67. Rearranges the elements in the range [first, last) into the lexicographically next greater permutation of elements. The comparisons of individual elements are performed using either operator&lt; for the first version, or comp for the second.
  68. </p>
  69. <p>
  70. A permutation is each one of the N! possible arrangements the elements can take (where N is the number of elements in the range). Different permutations can be ordered according on how they compare lexicographicaly to each other; The first such-sorted possible permutation (the one that would compare lexicographically smaller to all other permutations) is the one which has all its elements sorted in ascending order, and the largest has all its elements sorted in descending order.
  71. </p>
  72. <p>
  73. If the function can determine the next higher permutation, it rearranges the elements as such and returns true. If that was not possible (because it is already at the largest), it rearranges the elements according to the first permutation (sorted in ascending order) and returns false.
  74. </p>
  75. <p>
  76. Parameters
  77. </p>
  78. <p>
  79. first, last
  80. Bidirectional iterators to the initial and final positions of the sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
  81. comp
  82. Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument is to be considered less than the second argument.
  83. </p>
  84. <p>
  85. Return value
  86. </p>
  87. <p>
  88. true if the function could rearrange the object as a lexicographicaly greater permutation. Otherwise, the function returns false to indicate that the arrangement is not greater than the previous, but the lowest possible (sorted in ascending order).
  89. </p>
  90. <p>
  91. Example
  92. </p>
  93. <pre class="c code c++" style="font-family:monospace;"><span class="co1">// next_permutation</span>
  94. <span class="co2">#include &lt;iostream&gt;</span>
  95. <span class="co2">#include &lt;algorithm&gt;</span>
  96. using namespace std<span class="sy0">;</span>
  97. &nbsp;
  98. <span class="kw4">int</span> main <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  99. <span class="kw4">int</span> myints<span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="br0">&#123;</span>1<span class="sy0">,</span>2<span class="sy0">,</span>3<span class="br0">&#125;</span><span class="sy0">;</span>
  100. &nbsp;
  101. <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;The 3! possible permutations with 3 elements:<span class="es1">\n</span>&quot;</span><span class="sy0">;</span>
  102. &nbsp;
  103. sort <span class="br0">&#40;</span>myints<span class="sy0">,</span>myints<span class="sy0">+</span>3<span class="br0">&#41;</span><span class="sy0">;</span>
  104. &nbsp;
  105. <span class="kw1">do</span> <span class="br0">&#123;</span>
  106. <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0">&lt;&lt;</span> myints<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">&lt;&lt;</span> <span class="st0">&quot; &quot;</span> <span class="sy0">&lt;&lt;</span> myints<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> <span class="sy0">&lt;&lt;</span> <span class="st0">&quot; &quot;</span> <span class="sy0">&lt;&lt;</span> myints<span class="br0">&#91;</span>2<span class="br0">&#93;</span> <span class="sy0">&lt;&lt;</span> endl<span class="sy0">;</span>
  107. <span class="br0">&#125;</span> <span class="kw1">while</span> <span class="br0">&#40;</span> next_permutation <span class="br0">&#40;</span>myints<span class="sy0">,</span>myints<span class="sy0">+</span>3<span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
  108. &nbsp;
  109. <span class="kw1">return</span> <span class="nu0">0</span><span class="sy0">;</span>
  110. <span class="br0">&#125;</span></pre>
  111. <p>
  112. Output:
  113. </p>
  114. <p>
  115. 1 2 3
  116. </p>
  117. <p>
  118. 1 3 2
  119. </p>
  120. <p>
  121. 2 1 3
  122. </p>
  123. <p>
  124. 2 3 1
  125. </p>
  126. <p>
  127. 3 1 2
  128. </p>
  129. <p>
  130. 3 2 1
  131. </p>
  132. <p>
  133. Related Topics: <a href="../../stl/algorithm/prev_permutation.html" class="wikilink1" title="stl:algorithm:prev_permutation">prev_permutation</a>, <a href="../../stl/algorithm/random_sample.html" class="wikilink1" title="stl:algorithm:random_sample">random_sample</a>, <a href="../../stl/algorithm/random_sample_n.html" class="wikilink1" title="stl:algorithm:random_sample_n">random_sample_n</a>, <a href="../../stl/algorithm/random_shuffle.html" class="wikilink1" title="stl:algorithm:random_shuffle">random_shuffle</a>
  134. </p>
  135. </div>
  136. <div class="secedit"><form class="button btn_secedit" method="post" action="/wiki/stl/algorithm/next_permutation"><div class="no"><input type="hidden" name="do" value="edit" /><input type="hidden" name="lines" value="3-" /><input type="hidden" name="rev" value="1270640830" /><input type="submit" value="Edit" class="button" title="next_permutation" /></div></form></div>
  137. <!-- wikipage stop -->
  138. </div>
  139. <div class="clearer">&nbsp;</div>
  140. <div class="stylefoot">
  141. <div class="meta">
  142. <div class="user">
  143. </div>
  144. <!--
  145. <div class="doc">
  146. stl/algorithm/next_permutation.txt &middot; Last modified: 04/07/2010 04:47 by 60.191.2.238 </div>
  147. -->
  148. </div>
  149. <div class="bar" id="bar__bottom">
  150. <div class="bar-left" id="bar__bottomleft">
  151. <a href="../../stl/algorithm/next_permutation.html" class="action edit" accesskey="e" rel="nofollow">Edit this page</a> &#149;
  152. <a href="../../stl/algorithm/next_permutation.html" class="action revisions" accesskey="o" rel="nofollow">Old revisions</a> </div>
  153. <div class="bar-right" id="bar__bottomright">
  154. &#149;
  155. &#149;
  156. &#149;
  157. <a href="../../stl/algorithm/next_permutation.html" class="action login" rel="nofollow">Login</a> &#149;
  158. <a href="../../stl/algorithm/next_permutation.html" class="action index" accesskey="x" rel="nofollow">Index</a> &#149;
  159. <a href="../../stl/algorithm/next_permutation.html" class="action recent" accesskey="r" rel="nofollow">Recent changes</a> &#149;
  160. <a href="../../feed.php.html" title="Recent changes RSS feed">RSS</a> &#149;
  161. <a href='http://creativecommons.org/licenses/by/3.0/us/' title='Creative Commons license'>cc</a> &#149;
  162. <form action="/wiki/" accept-charset="utf-8" class="search" id="dw__search"><div class="no"><input type="hidden" name="do" value="search" /><input type="text" id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" /><input type="submit" value="Search" class="button" title="Search" /><div id="qsearch__out" class="ajax_qsearch JSpopup"></div></div></form>&nbsp;
  163. </div>
  164. <div class="clearer"></div>
  165. </div>
  166. </div>
  167. </div>
  168. <div class="no"><img src="/wiki/lib/exe/indexer.php?id=stl%3Aalgorithm%3Anext_permutation&amp;1273196562" width="1" height="1" alt="" /></div>
  169. </body>
  170. </html>