Vectors can write matrices over two dimensions. I was thinking about this problem when I was reading vector and string. How it should be written. Using a similar idea to a two-dimensional array, if the vector holds a vector that is not available, and the access operation is much more convenient than the array. The procedure is as follows:
#include <iostream>
#include <vector>
using namespace std;
Template<typename t>//Template
class Matrix {public
:
matrix (int rows, int cols): arr (rows)
{/
* for (rsize_t t = 0;t < rows;t++)
arr[t].resize (cols); */For
(auto &thisrow:arr)//auto automatically inferred
. Resize (cols);
}
Matrix (vector<vector<t>> V): arr{v} //Copy copy constructor
{}
matrix (vector<vector<t> > &&v): arr{std::move (v)}//Mobile copy constructor
{}
const vector<t> &operator[] (int row) const// Overloaded subscript reference
{return
arr[row];
}
vector<t>& operator[] (int row)
{return
arr[row];
}
int numrows () const//number of rows
{return
arr.size ();
}
int numcols () const //Number of columns
{return
numrows () arr[0].size (): 0;
}
Private:
vector<vector<t>>arr;
};
This is achieved.