#include #include using namespace std; template //三元组 struct Trituple { int row; int col; T val; }; //稀疏矩阵声明 template class SparseMatrix { public: SparseMatrix(int maxt=100); ~SparseMatrix(); bool TransposeTo(SparseMatrix &); bool AddTo(const SparseMatrix&); bool TransposeTo_Faster(SparseMatrix&); void Input(); void Output(); private: Trituple* data; int rows,cols,terms; int maxterms; }; template SparseMatrix::SparseMatrix(int maxt) { maxterms=maxt; data=new Trituple[maxterms]; terms=rows=cols=0; } template SparseMatrix::~SparseMatrix() { if (data!=NULL) { delete[] data; } } //普通转置 template bool SparseMatrix::TransposeTo(SparseMatrix &B) { if (terms>B.maxterms) { return false; } B.rows=cols; B.cols=rows; B.terms=terms; if (terms>0) { int p=0; for (int j=1;j<=cols;j++) { for (int k=0;k bool SparseMatrix::TransposeTo_Faster(SparseMatrix& B) { if (terms>B.maxterms) { return false; } B.rows=cols; B.cols=rows; B.terms=terms; if (terms>0) { int *num,*cpot; num=new int[cols]; cpot=new int[cols]; for (int j=0;j void SparseMatrix::Input() { cout<<"intput the matrix' row:"; int row1; cin>>row1; cout<<"intput the matrix' col:"; int col1; c...