#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
/* Just a helper function that prints the contents of the vector, separated by a space */
void print_vector(vector<int> data)
{
for (auto elem : data)
cout << elem << ' ';
}
/* Uses the <algorithm> function to generate permutations */
void print_permutations_cheesy(vector<int> data)
{
/* Permutations are generated in lexicographical order, sort to start with the "smallest" value */
sort(data.begin(), data.end());
do /* For every permutation.. */
{
/* Print the vector */
print_vector(data);
/* End the line */
cout << endl;
}
/* Calculate the next permutation */
while (next_permutation(data.begin(), data.end()));
}
/* The function that actually generates the permutations, and releases the "f" function on each one of them */
void permute(vector<int> data, const unsigned begin, const unsigned end, function<void(const vector<int>&)> f)
{
/* If begin equals end, it means we only look at a single element; there is nothing left to permute */
if (begin == end)
{
f(data);
}
else
{
/* Go past every element in the range [begin,end] */
for (unsigned i = begin; i <= end; i++)
{
/* Swap the first element with the i-th element */
swap(data[i], data[begin]);
/* Recursively permute the rest of the range */
permute(data, begin + 1, end, f);
/* Restore the array to it's original (as in: how it was supplied to this function) state */
swap(data[i], data[begin]);
}
}
}
/* Generate the permutations ourselves, this is just a wrapper function …
deceptikon commented: Cheesy or not, giving away the answer is unhelpful -3
PrimePackster commented: Again! No spoon feeding on DANIWEB +0
deceptikon commented: The OP's question falls under our homework rule. -2
nitin1 commented: not even single effort seen , yet you give the code instead of advices +0
WaltP commented: If you have a problem with the way things are supposed to be done, keep it out of the responses and make your points in the Feedback Forum. Stop cluttering up other people's threads with your rants. -3
iamthwee commented: I am Iamthwee and I disagree with this post. -3
iamthwee commented: Yes you deserve this +0
happygeek commented: some more well deserved neg rep from me... -2
WaltP commented: Then why did you bather doing his moework for him in the first place? -3