Compute all the permutations for a given vector of integers The task is to compute all the permutations for a given vector of integers (but of course the specific integer type is not relevant for the solution)

The strategy is based on recursion + iterations 

At each recursion, the state consists of  


the root sequence a which is the set of elements already placed 
the remaining elements set b which is the set of elements still to be placed 


Inside the recursion, a loop places the N(i) (with i recursion index and) remaining elements producing the same amount of new root sequences and a new recursion is started so that N(i+1)=N(i)-1 hence meaning the overall complexity is O(N!) as expected 

The recursion ends when there are no more elements to place hence b.empty() is true 

Each recursion set ends with a valid sequence hence they are all merged together in a final list of sequences 

Here is my CPP solution 

#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

vector&lt;int&gt; remove_item (const vector&lt;int&gt;&amp; a, const unsigned int id)
{
    vector&lt;int&gt; res; 
    for(unsigned int i=0; i&lt;a.size(); ++i) if(i!=id) res.push_back(a[i]); 
    return res;
}


vector&lt;int&gt; add_item(const vector&lt;int&gt;&amp; a, const int b)
{
    vector&lt;int&gt; res=a; 
    res.push_back(b); 
    return res;
}

vector&lt; vector&lt;int&gt; &gt; merge(const vector&lt; vector&lt;int&gt; &gt;&amp; a, const vector&lt; vector&lt;int&gt; &gt;&amp; b)
{
    vector&lt; vector&lt;int&gt; &gt; res=a; 
    for(const auto&amp; e : b) res.push_back(e); 
    return res;
}



vector&lt; vector&lt;int&gt; &gt; permutations(const vector&lt;int&gt;&amp; b, const vector&lt;int&gt;&amp; a={})
{

    if(b.empty()) return { a }; 

    vector&lt; vector&lt;int&gt; &gt; res; 
    for(unsigned int i=0; i&lt;b.size(); ++i) res=merge(res, permutations(remove_item(b,i), add_item(a, b[i]))); 
    return res; 
}

int main() {
    // your code goes here

    auto res = permutations({1,2,3,4,5}); 
    cout &lt;&lt; "Sol Num = " &lt;&lt; res.size() &lt;&lt; endl; 
    for(const auto&amp; a : res) 
    {
        for(const auto&amp; b : a) cout &lt;&lt; to_string(b) &lt;&lt; " "; 
        cout &lt;&lt; endl; 
    }
    return 0;
}
A cleaner solution is to trust the standard library and try to re-use the generic components already available there. Your problem is solved by std::next_permutation, so you can proceed along the lines of:

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

int main()
{
    std::vector&lt;int&gt; v = { 1, 2, 3, 4, 5 };

    do
    {
        for (auto e : v)
            std::cout &lt;&lt; e &lt;&lt; " ";
        std::cout &lt;&lt; "\n";
    } 
    while (std::next_permutation(v.begin(), v.end()));
}




For pedagogical purposes, if you wanted to keep your current structure, you could also use standard functions there. In particular, remove_item and merge could be rewritten to:

std::vector&lt;int&gt; remove_item(const std::vector&lt;int&gt;&amp; a, int id)
{
    assert(id &gt;= 0 &amp;&amp; id &lt; a.size());

    std::vector&lt;int&gt; res(a.begin(), a.begin() + id);
    res.insert(res.end(), a.begin() + id + 1, a.end());
    return res;
}

std::vector&lt;std::vector&lt;int&gt; &gt; merge(const std::vector&lt;std::vector&lt;int&gt; &gt;&amp; a, const std::vector&lt;std::vector&lt;int&gt; &gt;&amp; b)
{
    std::vector&lt;std::vector&lt;int&gt; &gt; res(a);
    std::copy(b.begin(), b.end(), std::back_inserter(res));
    return res;
}


Whatever you do, as general comments:


Avoid writing using namespace std;.
Don't write std::endl when \n will do.
You don't need std::to_string, just print b.
You are more likely to make mistakes when you put multiple statements on the same line. So instead of writing for(...) if(...) v.push_back(x); just write

for(...)
{
   if(...)
   {
      v.push_back(x);
   }
}


This also improves readability.