标签:iostream tran ++ end out nbsp std quick 矩阵转置
SparseMatrix.h
#pragma once #include<iostream> using namespace std; class Tri { public: int col; int row; int value; }; class SparseMatrix { public: int term; Tri* elements; int Srow; int Scol; SparseMatrix(int t = 2, int r = 10, int c = 10) { term = t; Srow = r; Scol = c; elements = new Tri[term]; } void quickTranspose(SparseMatrix& B) { int* rowCount; int* rowBegin; rowCount = new int[Scol]; rowBegin = new int[Scol]; for (int i = 0; i < Scol; i++) { rowCount[i] = 0; } for (int i = 0; i < term; i++) { rowCount[elements[i].col]++; } for (int i = 0; i < Scol; i++) { rowBegin[i] = 0; } for (int i = 1; i < Scol; i++) { rowBegin[i] = rowBegin[i - 1] + rowCount[i - 1]; } cout << "RowBegin:" << endl; for (int i = 1; i < Scol; i++) { cout << i << ":" << rowBegin[i] << " "; } cout << endl; //完成辅助数组 int index_B = 0; for (int i = 0; i < term; i++) { index_B = rowBegin[elements[i].col]; B.elements[index_B].col = elements[i].row; B.elements[index_B].row = elements[i].col; B.elements[index_B].value = elements[i].value; rowBegin[elements[i].col]++; } } void show() { for (int i = 0; i < term; 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].col = 9; A.elements[0].row = 0; A.elements[0].value = 8; A.elements[1].col = 0; A.elements[1].row = 6; A.elements[1].value = 6; A.show(); cout << "----------------------" << endl; A.quickTranspose(B); B.show(); return 0; }
标签:iostream tran ++ end out nbsp std quick 矩阵转置
原文地址:https://www.cnblogs.com/SlowIsFast/p/12689709.html