# Permutation of a vector

• 03-28-2005
tallgeese84
Permutation of a vector
I was hoping for a little help on this subject. I am trying to find all the permutations of a vector. Ive done a bit of research and could only find a recursive solution. Recursion still kind of confuses me, so I was wondering if anyone could help with a non recursive version? Heres what I have so far:

Code:

void perm(vector<int>v, int num, int curNum, int posNum, int &output)
{
v.insert(v.begin()+posNum, curNum);//puts the current number into the desired position

if(curNum == num)//stop case
{
print(v, num, output);//print function
return;
}

for(int i=0; i <= curNum; i++)
{
perm(v, num, curNum+1, i, output);//recursive call
}
}

• 03-29-2005
treenef
What exactly do you mean by finding all permutations of a vector?
Explain
• 03-29-2005
misplaced
this is the perfect chance to learn about recursion.
make this modification:
Code:

void perm(vector<int>v, int num, int curNum, int posNum, int &output, int level)
{
v.insert(v.begin()+posNum, curNum);//puts the current number into the desired position

if(curNum == num)//stop case
{
print(v, num, output, level);//print function
return;
}

for(int i=0; i <= curNum; i++)
{
perm(v, num, curNum+1, i, output, level++);//recursive call
}
}

• 03-29-2005
hk_mp5kpdw
Code:

#include <algorithm>
#include <vector>

...

std::vector<int> intVect;

...

std::sort(intVect.begin(),intVect.end());
print(...);      // Call your print function
while( std::next_permutation(intVect.begin(), intVect.end()) )
print(...);  // Call your print function

• 03-29-2005
tallgeese84
Thanks for the help everyone. This has helped me greatly.