The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
 lang="en" dir="ltr">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>
    next_permutation    [C++ Reference]
  </title>

  <meta name="generator" content="DokuWiki Release 2009-12-25c &quot;Lemming&quot;" />
<meta name="robots" content="index,follow" />
<meta name="date" content="2010-04-07T04:47:10-0700" />
<meta name="keywords" content="stl,algorithm,next_permutation" />
<link rel="search" type="application/opensearchdescription+xml" href="/wiki/lib/exe/opensearch.php" title="C++ Reference" />
<link rel="start" href="/wiki/" />
<link rel="contents" href="/wiki/stl/algorithm/next_permutation?do=index" title="Index" />
<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/wiki/feed.php" />
<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="/wiki/feed.php?mode=list&amp;ns=stl:algorithm" />
<link rel="edit" title="Edit this page" href="/wiki/stl/algorithm/next_permutation?do=edit" />
<link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/stl/algorithm/next_permutation" />
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/stl/algorithm/next_permutation" />
<link rel="canonical" href="http://www.cppreference.com/wiki/stl/algorithm/next_permutation" />
<link rel="stylesheet" media="all" type="text/css" href="/wiki/lib/exe/css.php?s=all&amp;t=custom1&amp;tseed=1272971091" />
<link rel="stylesheet" media="screen" type="text/css" href="/wiki/lib/exe/css.php?t=custom1&amp;tseed=1272971091" />
<link rel="stylesheet" media="print" type="text/css" href="/wiki/lib/exe/css.php?s=print&amp;t=custom1&amp;tseed=1272971091" />
<script type="text/javascript" charset="utf-8" ><!--//--><![CDATA[//><!--
var NS='stl:algorithm';var JSINFO = {"id":"stl:algorithm:next_permutation","namespace":"stl:algorithm"};
//--><!]]></script>
<script type="text/javascript" charset="utf-8" src="/wiki/lib/exe/js.php?tseed=1272971091" ></script>

  <link rel="shortcut icon" href="/wiki/lib/tpl/custom1/images/favicon.png" />

  </head>

<body>
<div class="dokuwiki">
  
  <div class="stylehead">

    <div class="breadcrumbs">
      <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>
    
  </div>


  
  
  <div class="page">

    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
    <!-- wikipage start -->
    


<h2><a name="next_permutation" id="next_permutation">next_permutation</a></h2>
<div class="level2">

<p>
Syntax:
</p>
<pre class="c code c++" style="font-family:monospace;">    <span class="co2">#include &lt;algorithm&gt;</span>
    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>
    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>
<p>
Transform range to next permutation
</p>

<p>
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.
</p>

<p>
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.
</p>

<p>
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.
</p>

<p>
Parameters
</p>

<p>
first, last
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.
comp
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.
</p>

<p>

Return value
</p>

<p>
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).
</p>

<p>
Example
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="co1">// next_permutation</span>
<span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;algorithm&gt;</span>
using namespace std<span class="sy0">;</span>
&nbsp;
<span class="kw4">int</span> main <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <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>
&nbsp;
  <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>
&nbsp;
  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>
&nbsp;
  <span class="kw1">do</span> <span class="br0">&#123;</span>
    <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>
  <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>
&nbsp;
  <span class="kw1">return</span> <span class="nu0">0</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre>
<p>
Output:
</p>

<p>
1 2 3
</p>

<p>
1 3 2
</p>

<p>
2 1 3
</p>

<p>
2 3 1
</p>

<p>
3 1 2
</p>

<p>
3 2 1
</p>

<p>

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>
</p>

</div>

    <!-- wikipage stop -->
  </div>

  <div class="clearer">&nbsp;</div>

  
  <div class="stylefoot">

    <div class="meta">
      <div class="user">
              </div>
      <!--
      <div class="doc">
        stl/algorithm/next_permutation.txt &middot; Last modified: 04/07/2010 04:47 by 60.191.2.238      </div>
      -->
    </div>

   
    </div></div></body>
</html>