0

I want to initialize a Identity Matrix (ones on the main diagonal and zeros elsewhere) with a 2-D array. I try to implement this with vector. The code is like this:

vector<vector<int> > matrix;
matrix.resize(n);
for(auto &i: matrix)
    i.resize(n);

// initialize...

This is far more complex than just with ordinary array with just int matrix[n][n]. I don't do this because I want to return this 2-D matrix. So the function is like this :

matrix_type
func(int n)
{
  //intialize the matrix
  ...
}

Because the array in C++11 is like this:

array<Elem,N> arr;

I have no idea how to get the N at runtime. Then I decide to use the vector.

So is there is a better way to handle this?(without using traditional array)


Thanks for the answer. The question can be solved by use

vector<vector<int>> matrix(n, std::vector<int>(n));

And , another question. If I am a stupid guy and I just want to use the interface, that is , I want the return type to be array<int,n>. So is there any method to get the N at runtime and return it?

Sut
  • 67
  • 7
  • What is the reason you cannot use the 2D array approach? All of these methods would work for what you need, which one would you prefer to implement? – Easton Bornemeier Jul 10 '18 at 14:50
  • 1
    The ordinary `int matrix[n][n]` doesn't work with `n` unknown at compile time as well. – V. Kravchenko Jul 10 '18 at 14:51
  • Oh this is just for curiosity. Since STL has defined a new interface. So I am wondering if there is a good method to solve this problem by using just STL. – Sut Jul 10 '18 at 14:52
  • @V.Kravchenko Yes that't true. Thanks for pointing out. Then things get more complicated~~ – Sut Jul 10 '18 at 14:53
  • Note that a vector of vectors is an inefficient representation of a matrix. A flat vector of rows stored consequetively is better. – eerorika Jul 10 '18 at 14:57
  • @user2079303 Yes, that's true. But the code to access a matrix is much more natural with a 2-D array. – Sut Jul 10 '18 at 15:08

2 Answers2

2

You can simplify the construction/initialization of the matrix to:

vector<vector<int>> matrix(n, std::vector<int>(n));

The above line will initialize all the elements of the matrix to 0. You can set the values of the diagonal elements to 1 using a for loop.

for ( size_t i = 0; i < n; ++i )
{
   matrix[i][i] = 1;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You can initialize a 2D array of std::vector<T> as follows:

 template<typename T>
 using Matrix = std::vector<std::vector<T>>;

 template<typename T>
 Matrix<T> make2DArray(int N, int M){
     return Matrix<T>(N, std::vector<T>(M));
 }

Calling it like:

 auto my2Darr = make2DArray<int>(20, 30);
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
  • There are too much of std::vector> in this solution, consider making a typedef `typedef Matrix std::vector>;` or if template T is really needed `template using Matrix = std::vector>;` – V. Kravchenko Jul 10 '18 at 14:53
  • @V.Kravchenko, Good catch. updated. Thanks .. – WhiZTiM Jul 10 '18 at 15:00