-1

I'm a beginner in C++.

I'm trying to make a Matrix class by my own to train me. I declared in my class Matrix std::vector<std::vector<long>> m_data; whitch is a 2D vector to store my numbers. In a constructor I want to initialize my 2D array with 2 arguments : NbRows and NbCols. I'm stuck there.

Matrix::Matrix(unsigned int nb_row, unsigned int nb_col) 

{
    int i, j;
    m_nbRow = nb_row;
    m_nbCol = nb_col;

    for(i = 0; i < m_nbCol; i ++)
    {
        for(j = 0; j <m_nbRow; j ++)
        {
            // ?????
        }
    }
}
Icetom54
  • 55
  • 2
  • 7
  • Vectors don't have a fixed size. They are sized as needed, typically when using `push_back()`. Use `std::array` if you want a fixed array size. http://en.cppreference.com/w/cpp/container/array – Drise Mar 26 '18 at 19:02
  • This is also a good place to use a [member initializer list](http://en.cppreference.com/w/cpp/language/initializer_list) – user4581301 Mar 26 '18 at 19:06
  • This has been asked many, many times. Please research before asking so that we are not repeating ourselves. – Lightness Races in Orbit Mar 26 '18 at 21:07

3 Answers3

3

A simple one-line solution is as follows:

Matrix(unsigned int nb_row, unsigned int nb_col){
    m_data = std::vector<std::vector<long>>(nb_row, std::vector<long>(nb_col,0));
}

You can replace the "0" with any other number, depending on what you wish to initialise your matrix to.

Thomas English
  • 231
  • 1
  • 8
2

You have to resize the outer vector before the first for loop.

The inner vector can be resized and then assigned to, or items pushed back to it.

Matrix::Matrix(unsigned int nb_row, unsigned int nb_col) 
{
    int i, j;
    m_nbRow = nb_row;
    m_nbCol = nb_col;

    m_data.resize(m_nbCol);
    for(i = 0; i < m_nbCol; i ++)
    {
        for(j = 0; j <m_nbRow; j ++)
        {
            m_data[i].push_back(<some value>);
        }
    }
}

or

Matrix::Matrix(unsigned int nb_row, unsigned int nb_col) 
{
    int i, j;
    m_nbRow = nb_row;
    m_nbCol = nb_col;

    m_data.resize(m_nbCol);
    for(i = 0; i < m_nbCol; i ++)
    {
        m_data[i].resize(m_nbRow);
        for(j = 0; j <m_nbRow; j ++)
        {
            m_data[i][j] = <some value>;
        }
    }
}

If you want to initialize the elements of the matrix to zero, you can simplify it to:

Matrix::Matrix(unsigned int nb_row, unsigned int nb_col) 
{
    m_nbRow = nb_row;
    m_nbCol = nb_col;
    m_data.resize(m_nbCol, std::vector<long>(m_nbRow));
}

PS

Normally, the first index of matrix corresponds to a row while the second index corresponds to a column. You are using them the other way around in your post. I assume you have a reason for that.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

The shortest solution ever:

Matrix(unsigned int nb_row, unsigned int nb_col) : m_nbrow(nb_row), m_nbcol(nb_col), 
     m_data(nb_row, vector<int>(nb_col)) {}

NB vector of vectors is probably not the best idea as it will create double indirection and it will store and check redundant copies of the inner matrix dimension size (i.e. the size of inner vectors), not to mention the non-sequential layout.

A more common approach is one vector of a size nb_row*nb_col with some usual syntactic trickery for indexing - index = nb_col*y + x.

Ap31
  • 3,244
  • 1
  • 18
  • 25