Click here to Skip to main content
12,896,175 members (50,545 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 13 Aug 2011

C++ Permutation Class

, 13 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A C++ class that can perform k-permutations of n on a set


This class can perform permutations on a set, but not the kind of permutations that the STL next_permutation() function does, which are permutations of n, but k-permutations of n.


There is some confusion over the meaning of "permutation" so for clarity, you might want to read the Wikipedia article on permutations. Note that this does not calculate the number of permutations there are in given parameters of k and n but returns all the subsets that form the permutations of a set.

Using the Code

Currently, you do not get to choose the set. AiPermutations presumes that your set will be all integers between 0 and n inclusive. If that does not suit your data, then you can treat the arrays returned as holding the indexes to the array of your set. So, if your set would look something like:

char myset[] = { 'A', 'B', 'C', 'D' };

then you can convert the returned subset into a subset of your liking with:

for(int i = 0; i < k; i++)
	mysubset[i] = myset[ returnedsubset[i] ];

Other than that, usage is very simple. Just plug the k and n parameters into the constructor, allocate an elementtype-array of size k and pass it to AiPermutations::next(), which you repeatedly call until it returns false. Here's an example:

#include <span class="code-keyword"><iostream>
using namespace std;

#include <span class="code-string">"aipermutations.h"
</span>#include <span class="code-keyword"><boost/scoped_array.hpp>
</span>#include <span class="code-keyword"><stdlib.h>
#define K 3
#define N 6

int fact(int n) {
	int nAccumulator = 1;
	for(int i = 2; i <= n; i++)
		nAccumulator *= i;
	return nAccumulator;

int main(int argc, char **argv) {
	AiPermutations perm(K, N);
		permutation(new AiPermutations::elementtype[K]);

	int nPermutationCount = 0;
	while ( {
		cout << "got: (";
		for(int i = 0; i < K - 1; i++)
			cout << permutation[i] << ", ";
		cout << permutation[K - 1] << ')' << endl;

	cout << "Finally, " << nPermutationCount << " 
	permutations when it should have been: " << fact(N)/fact(N - K) << endl;



This class is not designed around it. It will not throw exceptions or return errors (when built in release mode) for absurd parameters or inadequately allocated arrays in method parameters. Furthermore, the code will behave unpredictably when processing trivial examples, such as a k = 0, even if they are mathematically sound.


It should be portable in ANSI C and requires STL. So far I've built it under:

  • gcc 4.4.3 (Ubuntu 10.04)
  • Visual Studio 2005 (WinXP)

Watch for...

...a template version that allows the user to use sets of his own type. This should not be difficult, since the concept is as difficult as the example above, however if a set was provided with duplicate elements, then non-unique subsets would be produced, which probably could not be managed easily.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Canada Canada
Angus March is a computer engineer in Ottawa.

You may also be interested in...

Comments and Discussions

Questionnice Pin
CIDev20-Sep-11 2:54
memberCIDev20-Sep-11 2:54 
QuestionYou need a template version? Pin
mbue15-Aug-11 13:39
membermbue15-Aug-11 13:39 
AnswerRe: You need a template version? Pin
AngusEm15-Aug-11 15:47
memberAngusEm15-Aug-11 15:47 
GeneralRe: You need a template version? Pin
mbue15-Aug-11 16:21
membermbue15-Aug-11 16:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 13 Aug 2011
Article Copyright 2011 by AngusEm
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid