「よりスマートな」ループと「サナー」のどちらが好きですか?
//c++ --std=c++11 test.cc
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <utility>
using std::string;
using std::list;
using std::vector;
const string show[8] = { "(-1,-1)","( 0,-1)","( 1,-1)"
, "(-1, 0)", "( 1, 0)"
, "(-1, 1)","( 0, 1)","( 1, 1)"
};
auto permutations_of_living_cells =
[] (int number_of_living_cells) -> list<vector<string>>
{
typedef list<vector<string>> (*recT)( void*
, int
, int
, vector<string> &&
, list<vector<string>> &&
);
recT rec = []( void*r
, int n
, int i
, vector<string> && prefix
, list<vector<string>> && l
) -> list<vector<string>>
{
if( n==0 )
{
l.push_back(std::move(prefix));
return std::move(l);
}
if( i>8-n ) return std::move(l);
vector<string> psi(prefix);
psi.push_back(show[i]);
return ((recT)r)(r,n ,i+1,std::move(prefix),
((recT)r)(r,n-1,i+1,std::move(psi ),
std::move(l)
)
);
};
return rec( (void*)rec
, number_of_living_cells
, 0
, vector<string>()
, list<vector<string>>()
);
};
template<class T>
std::ostream& operator<<( std::ostream & out,const vector<T> & v )
{
if( v.empty() ) return out << "[]";
out << "[ " << *v.begin();
std::for_each( v.begin()+1, v.end(), [&](T x){out<<", "<<x;} );
return out << " ]";
}
int main()
{
for( auto v : permutations_of_living_cells(3) )
std::cout << v << "\n";
std::cout << std::flush;
return 0;
}