Instead of making an array as you have tried to do, you could make a class template that will construct a matrix like object for you. Here is what I have come up with, now the overall design or pattern of this template will fit your condition but the actual implementation to generate the internal matrix will depend on your data and what you intend.
#include <vector> #include <iostream> #include <conio.h> template <class T, class U> class Matrix { private: std::vector<T> m_lines; std::vector<T> m_cols; std::vector<U> m_mat; std::size_t m_size; std::size_t m_lineCount; std::size_t m_colsCount; public: Matrix() {}; Matrix( const std::vector<T>& lines, const std::vector<T>& cols ) : m_lines(lines), m_cols(cols), m_lineCount( lines.size() ), m_colsCount( cols.size() ) { addVectors( lines, cols ); } void addVectors( const std::vector<T>& v1, const std::vector<T>& v2 ) { m_lines = v1; m_cols = v2; m_lineCount = m_lines.size(); m_colsCount = m_cols.size(); for ( unsigned int i = 0; i < m_lineCount; ++i ) { for ( unsigned int j = 0; j < m_colsCount); j++ ) { // This will depend on your implementation and how you // construct this matrix based off of your existing containers m_mat.push_back(m_lines[i] & m_cols[j]); } } m_size = m_mat.size(); } std::size_t size() const { return m_size; } std::size_t sizeRows() const { return m_lineCount; } std::size_t sizelColumns() const { return m_colsCount; } std::vector<U>& getMatrix() const { return m_mat; } std::vector<T>& getLines() const { return m_lines; } std::vector<T>& getColumns() const { return m_columns; } bool operator[]( std::size_t idx ) { return m_mat[idx]; } const bool& operator[]( std::size_t idx ) const { return m_mat[idx]; } }; int main() { std::vector<unsigned> v1{ 1, 0, 1, 1, 0 }; std::vector<unsigned> v2{ 0, 1, 1, 1, 0 }; Matrix<unsigned, bool> mat1( v1, v2 ); int line = 0; for ( unsigned u = 0; u < mat1.size(); ++u ) { line++; std::cout << mat1[u] << " "; if ( line == mat1.sizeRows() ) { std::cout << "\n"; line = 0; } } std::cout << "\nPress any key to quit.\n" << std::endl; _getch(); return 0; }
Output
0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 0 0 0 0
With this template class you can create a matrix of any type U by passing in two vectors for type T. Now how you construct the matrix will be implementation dependent. But this class is reusable for different types.
You could have two vectors of type doubles, and construct a matrix of unsigned chars, or you could have two vectors of user defined class or struct types and generate a matrix of unsigned values. This may help you out in many situations.
Note: - This does generate a compiler warning, no errors though and it prints and displays properly, but the compiler warning generated by MSVS 2015 is warning C4800: unsigned int: forcing value to bool true or false (performance warning)
This is generated for I am doing a bit wise & operation on to unsigned values; but that is why I set my initial vectors to be passed to this class template's constructor to have all 1s & 0s as this is meant for demonstration only.
EDIT - I made an edit to the class because I noticed I had a default constructor and had no way to add vectors to it, so I added an extra member variable, and an addVectors function, and moved the implementation from the defined constructor to the new function and just ended up calling that function in the defined constructor.
std::vector(more specifically, a vector of vectors).int array[SOME_SIZE] = { 0 };?