I have spent a decent amount of (waaay too much) time trying to write the below function and get my head around recursion. The aim was to return all permutations of an int vector (assuming no duplications of input). I would like to know people's thoughts on my code. One downfall that I can identify is that it is pretty demanding of space since I am copying choice
and out
vectors many times.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
void uni_permutes(std::vector<int> in, std::vector<int> out, std::vector<int> choice)
{
int size = choice.size();
//complete when all ints in choice have already been used
if(size == 0) {
for(int i : out) {
std::cout << i << std::endl;
}
std::cout << '\n' << std::endl;
return;
}
std::vector<int> newo = out;
std::vector<int> newc = choice;
for (int i : choice) {
newo.push_back(i);
//remove i from choice newc.erase(std::remove(newc.begin(), newc.end(), i), newc.end());
uni_permutes(in,newo, newc);
newc = choice;
newo = out;
}
return;
}
int main()
{
std::vector<int> v1 = {0, 1,2,3};
std::vector<int > out;
uni_permutes(v1,out,v1);
return 0;
}
std::next_permutation
at least for inspiration. \$\endgroup\$ – Edward Oct 27 '16 at 1:01