I'm trying to build a generic matrix based on a generic vector that I've already built. I want to have a vector of vectors (each internal vector represents a row). For some reason, this isn't working:
template <typename T>
class matrix : vector<vector<T>>{...}
I'm getting this error:
error: 'class vector<T>' is not a valid type for a template non-type parameter
I tried looking into template templates, but couldn't really understand how to make them work. Any help would be much appreciated. Thanks!
This should work for you:
#include <vector>
template <typename T>
class matrix : public std::vector<std::vector<T>>
{
};
int main()
{
matrix<int> m;
m.push_back({});
m[0].push_back(0);
m[0].push_back(1);
m[0].push_back(2);
m.push_back({});
m[1].push_back(3);
m[1].push_back(4);
m[1].push_back(5);
m.push_back({});
m[2].push_back(6);
m[2].push_back(7);
m[2].push_back(8);
return 0;
}
However I suggest investigating "A proper way to create a matrix in c++" topic which looks very close to what your want to implement.