#include <iostream> #include <vector> #include <functional> #include <algorithm> 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){ std::rotate(a.begin(), a.begin() + 1, a.end());//rotate for left.fix the method spec. vecvec.push_back(a); } else{ for (int x = 1; x < m; 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 = 1; j < m; j++) PermSub(2, { j, 0}); return vecvec; } template<class T> bool MakeHoge(int M,T Base=T()){ auto R = Perm(M); std::cout << "first:" << M << " Count!"<<std::endl; 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; } template<class T> bool MakeHoge(const std::vector<T>& vec){ auto R = Perm(vec.size()); std::cout<<"First:" << '['; for (auto& o : vec) std::cout << o << ','; std::cout << ']'; std::cout << std::endl; for (auto& oo : R){ std::cout << '['; for (auto& o : oo) std::cout << vec[o] << ','; std::cout << ']'; std::cout << std::endl; } std::cout << std::endl; std::cout << std::endl; return true; } int main(){ MakeHoge<int>({ 1, 2, 3, 4, 5 }); MakeHoge<char>({ 'a', 'B', 'c', 'D','e' }); MakeHoge<int>(5); MakeHoge<char>(5, 'a'); return 0; }
Standard input is empty
First:[1,2,3,4,5,] [1,2,4,5,3,] [1,2,5,4,3,] [1,2,3,5,4,] [1,2,5,3,4,] [1,3,2,5,4,] [1,3,5,2,4,] [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,5,] First:[a,B,c,D,e,] [a,B,D,e,c,] [a,B,e,D,c,] [a,B,c,e,D,] [a,B,e,c,D,] [a,c,B,e,D,] [a,c,e,B,D,] [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,e,] first:5 Count! [0,1,3,4,2,] [0,1,4,3,2,] [0,1,2,4,3,] [0,1,4,2,3,] [0,2,1,4,3,] [0,2,4,1,3,] [0,1,2,3,4,] [0,1,3,2,4,] [0,2,1,3,4,] [0,2,3,1,4,] [0,3,1,2,4,] [0,3,2,1,4,] first:5 Count! [a,b,d,e,c,] [a,b,e,d,c,] [a,b,c,e,d,] [a,b,e,c,d,] [a,c,b,e,d,] [a,c,e,b,d,] [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,e,]