12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
#include <iostream> #include <limits> #include <vector> #include <algorithm> constexpr unsigned long long ubound( std::size_t nbits ) { if( nbits < 2 ) return 2 ; else return ubound( nbits-1 ) * 2 ; } std::vector< std::vector<int> > generate_mtx( std::size_t nbits ) { nbits %= std::numeric_limits<unsigned long long>::digits ; std::vector< std::vector<int> > result(nbits) ; // note: col with all zeroes is skipped (start with number = 0 to include it) for( unsigned long long number = 1 ; number < ubound(nbits) ; ++number ) { auto n = number ; for( auto& vec : result ) { vec.push_back( n%2 ) ; n /= 2 ; } } // to get the rows in the same order as illustrated in the example std::reverse( std::begin(result), std::end(result) ) ; return result ; } int main() { for( std::size_t nbits = 2 ; nbits < 7 ; ++nbits ) { std::cout << "nbits == " << nbits << '\n' ; for( const auto& row : generate_mtx(nbits) ) { for( int v : row ) std::cout << v << ' ' ; std::cout << '\n' ; } std::cout << "\n-------------\n\n" ; } }
12
constexpr unsigned long long ubound( std::size_t nbits ) { return nbits < 2 ? 2 : ubound( nbits-1 ) * 2 ; }
body of constexpr function ‘...’ not a return-statement