#include <iostream> #include <vector> #include <functional> std::vector<std::vector<int>> Perm(int m){//this method is not my idea.i am confusing. std::vector<std::vector<int>> vecvec; std::function<void(int, std::vector<int>)> PermSub = [&](int n, std::vector<int> a){ bool IsFind = false; if (n == m){ vecvec.push_back(a); } else{ for (int x = 2; x < m + 1; x++){ IsFind = false; for (auto& i : a){ if (x == i){ IsFind = true; break; } } if (IsFind == false){ if (n != 2 || a[0]>x){ a.push_back(x); PermSub(n + 1, a); a.pop_back(); } } } } }; for (int j = 2; j < m + 1; j++) PermSub(2, { j, 1 }); return vecvec; } template<class T> bool MakeHoge(int M,T Base=T()){ auto R = Perm(M); if (Base != 0) Base--; for (auto& oo : R){ std::cout << '['; for (auto& o : oo) std::cout << static_cast<T>(o+Base) << ','; std::cout << ']'; std::cout << std::endl; } std::cout << std::endl; std::cout << std::endl; return true; } int main(){ MakeHoge<int>(5); MakeHoge<char>(5, 'a'); return 0; }
Standard input is empty
[3,1,2,4,5,] [3,1,2,5,4,] [4,1,2,3,5,] [4,1,2,5,3,] [4,1,3,2,5,] [4,1,3,5,2,] [5,1,2,3,4,] [5,1,2,4,3,] [5,1,3,2,4,] [5,1,3,4,2,] [5,1,4,2,3,] [5,1,4,3,2,] [c,a,b,d,e,] [c,a,b,e,d,] [d,a,b,c,e,] [d,a,b,e,c,] [d,a,c,b,e,] [d,a,c,e,b,] [e,a,b,c,d,] [e,a,b,d,c,] [e,a,c,b,d,] [e,a,c,d,b,] [e,a,d,b,c,] [e,a,d,c,b,]