Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



STL Programming from the Ground Up    (ISBN: 0078825075)


 

 List Price: $39.99
 Our Price: $27.99
 Used Price: $10.97

 Release Date: 01 December, 1998
 Manufacturer: McGraw-Hill Osborne Media (Paperback)
 Sales Rank: 46,500

 Author: Herbert Schildt, Herb Schildt









More Info

 STL Permutations 2003-05-29 00:05:00
  cpp 
Category: source:cpp:stl
Description: Example on using next_permutation from the stl algorithm library. It will rearrange a string or integer into every unique lexical order.
Platform: all
Author: detour
Viewed: 5599
Rating: 3.9/5 (32 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
permutations.cpp
/* permutation.cpp by detour@metalshell.com
 *
 * Example on using next_permitation from the stl
 * algorithm library.  It will rearrange a string or
 * integer into every unique lexical order.
 *
 * http://www.metalshell.com/
 *
 */

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> v_int;
  char str[] = "mobscene";

  v_int.push_back(1);
  v_int.push_back(4);
  v_int.push_back(2);
  v_int.push_back(0);

  // Print each unique order of "mobscene"

  while(next_permutation(str, str+strlen(str)))
    cout << str << "\t";

  cout << "\n\n";

  /* when calling next_permutation you must sort the vector
     values into ascending order for it work. */
  sort(v_int.begin(), v_int.end());

  /* Use a do/while loop instead of the normal while loop becuase
     the first entry will be skipped if you call next_permutation
     first */
  int cnt = 0;
  do {
    // There should be n! (4*3*2*1) entries.

    cout << ++cnt << ". ";

    // Print out the vector.

    for(unsigned int x = 0; x < v_int.size(); x++)
      cout << v_int[x];

    cout << endl;
  } while(next_permutation(v_int.begin(), v_int.end()));


  return 0;
}
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?