4.3.2 稀疏矩阵转置
Posted slowisfast
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4.3.2 稀疏矩阵转置相关的知识,希望对你有一定的参考价值。
SparsrMatrix.h
#pragma once #include<iostream> using namespace std; class Tri { public: int row; int col; int value; }; class SparseMatrix { public: int sRow; int sCol; int sTerm; Tri* elements;//to save terms. SparseMatrix(int r = 10, int c = 10, int term = 2) { sRow = r; sCol = c; sTerm = term; elements = new Tri[sTerm]; } void transpose(SparseMatrix& B) { int index_B = 0; if (sTerm > 0) { for (int i = 0; i < sCol; i++) { for (int j = 0; j < sTerm; j++) { if (elements[j].col == i) { B.elements[index_B].col = elements[j].row; B.elements[index_B].row = elements[j].col; B.elements[index_B].value = elements[j].value; index_B++; } } } } } void show() { for (int i = 0; i < sTerm; i++) { cout << elements[i].row << ‘,‘ << elements[i].col << ‘,‘ << elements[i].value << endl; } } };
main.cpp
#include"SparseMatrix.h" int main() { SparseMatrix A, B; A.elements[0].row = 1; A.elements[0].col = 3; A.elements[0].value = 6; A.elements[1].row = 5; A.elements[1].col = 1; A.elements[1].value = 8; A.show(); cout << endl; cout << "----------------------------" << endl; cout << endl; A.transpose(B); B.show(); return 0; }
以上是关于4.3.2 稀疏矩阵转置的主要内容,如果未能解决你的问题,请参考以下文章