Efficiently Generate Subset of all Permutations or Combinations with and without Repetition (C++) Suppose I want to write some functions which return the vector containing a vector representing all permutations or combinations with or without repetition (dependent on the function) of a subset of a given size of a given input vector.  Right now, my code is looking like this:

#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;iostream&gt;

template &lt;typename T&gt;
struct permcomb
{
  std::vector&lt;std::vector&lt;T&gt;&gt; end_set;
  std::vector&lt;T&gt;* data;
  permcomb(std::vector&lt;T&gt;&amp; param) : data(&amp;param) {}

  void permutation_help(std::vector&lt;T&gt; seen, int recurse_num, bool repetition, bool comb)
  {
    if(recurse_num == 0)
    {
      if(end_set.size() &gt; 0 &amp;&amp; comb)
      {
        bool f = false;
        for(int i = 0; i &lt; end_set.size(); i++)
        {
          if(std::is_permutation(end_set[i].begin(), end_set[i].end(), seen.begin()))
          {
            f = true;
          }
        }
        if(!f)
        {
          end_set.push_back(seen);
        }
      }
      else // permutations always choose this one
      {
        end_set.push_back(seen);
      }
    }
    else
    {
      for(int i = 0; i &lt; (*data).size(); i++)
      {
        if(repetition || find(seen.begin(), seen.end(), (*data)[i]) == seen.end()) // if not found
        {
          seen.push_back((*data)[i]);
          permutation_help(seen, recurse_num - 1, repetition, comb);
          seen.pop_back();
        }
      }
    }
  }
};

// return all permutations no repetition
template &lt;typename T&gt;
std::vector&lt;std::vector&lt;T&gt;&gt; rapnr(std::vector&lt;T&gt;&amp; data, int subset_size)
{
  permcomb&lt;T&gt; helpstruct(data);
  std::vector&lt;T&gt; empty {};
  helpstruct.permutation_help(empty, subset_size, false, false);
  return helpstruct.end_set;
}

// return all permutations with repetition
template &lt;typename T&gt;
std::vector&lt;std::vector&lt;T&gt;&gt; rapwr(std::vector&lt;T&gt;&amp; data, int subset_size)
{
  permcomb&lt;T&gt; helpstruct(data);
  std::vector&lt;T&gt; empty {};
  helpstruct.permutation_help(empty, subset_size, true, false);
  return helpstruct.end_set;
}

// return all combinations no repitition
template &lt;typename T&gt;
std::vector&lt;std::vector&lt;T&gt;&gt; racnr(std::vector&lt;T&gt;&amp; data, int subset_size)
{
  permcomb&lt;T&gt; helpstruct(data);
  std::vector&lt;T&gt; empty {};
  helpstruct.permutation_help(empty, subset_size, false, true);
  return helpstruct.end_set;
}

// return all combinations with repetition
template &lt;typename T&gt;
std::vector&lt;std::vector&lt;T&gt;&gt; racwr(std::vector&lt;T&gt;&amp; data, int subset_size)
{
  permcomb&lt;T&gt; helpstruct(data);
  std::vector&lt;T&gt; empty {};
  helpstruct.permutation_help(empty, subset_size, true, true);
  return helpstruct.end_set;
}


This seems to be giving me the right answers so far... but I'm not really happy with the way I went about generating the combinations.  Literally the only thing I could think of doing was generating all of the permutations and then eliminating all but one of those which were permutations of each other.  This clearly doesn't seem very efficient, but I was at a loss for else I could do.  Similarly, I don't feel as I generated the permutations that well either since I always have to go back and search for items that I've already inserted to make sure that I don't insert them again (in the case that repetition isn't allowed).  So what steps can I take to optimize what I have so far?
I'm going to focus on the algorithmic aspects of your code rather than the style, as I don't know C++. You want to generate permutations and combinations with unique elements, so all you need to do is find the sublist of unique elements, and find the permutations and combinations of that. To uniquify, you should use a structure with a fast contains method (tree or hash set), and add each element is the vector to it if it isn't already. This will use O(n) memory, but your output is much later, so that overhead is justified given that it will save a lot of time later.