templates - How can I create a sparse matrix as a list of lists? (C++) -
the question is: possible create sparse matrix using following sparse list implementation? in special, using class template class template (sparselist*>)?
i've created class template named sparselist can add elements in whatever index want.
i'd use create sparsematrix class template. tried following...
//sparsematrix.h template <typename t> class sparsematrix { public: sparsematrix(); private: sparselist<sparselist<t>*> *matrix; }; template <typename t> sparsematrix<t>::sparsematrix() { matrix = new sparselist<sparselist<t>*>(); }
but when try instantiate on main...
int main() { sparsematrix<int> *matrix; matrix = new sparsematrix<int>(); //without line compiled normally. return 0; }
i got following error...
in file included src/main.cpp: sparsematrix.h: instantiated 'sparsematrix<t>::sparsematrix() [with t = int]' main.cpp: instantiated here sparselist.h: error: template argument required 'struct sparsematrix'
i'm using netbeanside 6.9.1 mingw.
edit:
//sparselist.h template <typename t> class sparselist { template <typename u> friend std::ostream & operator<<(std::ostream &output, const sparselist<u> &list); public: sparselist(); virtual ~sparselist(); void insert(t &entry, int index); t & get(int i); int length(); private: struct listnode { int index; t *entry; listnode *next; }; listnode *head; //pointer first entry in sparse list. int size; //# of entries. };
i tested inserts , gets, constructors , destructors, in sparselist. working fine... =)
why pointers?
this should job data storage inside class:
std::map<std::pair<i, i>, t>
where i
index type (e.g. int
) , t
number type (e.g. double).
or use compressed_matrix
boost::ublas
.
Comments
Post a Comment