#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;
}