I guess you are looking for std::next_permutation()
:
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> simple{1, 2, 3};
do
{
for (auto e : simple) { std::cout << e << " "; }
std::cout << std::endl;
}
while (next_permutation(simple.begin(), simple.end()));
}
Here is a live example.
If you do not want to start with a sorted vector, you can use std::next_permutation()
the following way:
#include <iostream>
#include <algorithm>
#include <vector>
constexpr int factorial(int i)
{
return i == 0 ? 1 : i * factorial(i-1);
}
int main()
{
std::vector<int> simple{3, 1, 2};
for (int i = 0; i < factorial(simple.size()); i++)
{
std::next_permutation(simple.begin(), simple.end());
for (auto e : simple) { std::cout << e << " "; }
std::cout << std::endl;
}
}
Here is a live example.
Notice, that if the size of the vector is known at compile-time, as seems to be the case from your example, you could use std::array
instead of std::vector
, as shown in this live example.