c语言压缩存储矩阵转置运算,c++:稀疏矩阵的压缩存储及转置

SparseMatrix.hpp#pragma once

#include

using namespace std;

#include

template

struct Triple{

T _value;

size_t _row;

size_t _col;

};

#define ROW 6

#define COL 5

template

class SparseMatrix{

public:

SparseMatrix(int* matrix,size_t row,size_t col,const T& invalid):_row(row),_col(col){

for (size_t i = 0; i 

for (size_t j = 0; j 

if (matrix[i*col + j] != invalid){

Triple t;

t._row = i;

t._col = j;

t._value = matrix[i*col + j];

_array.push_back(t);

}

}

}

}

void Display(){

size_t index = 0;

for (int i = 0; i 

for (int j = 0; j 

if (index 

&& i == _array[index]._row

&& j == _array[index]._col){

cout <

index++;

}

else

cout <

}

cout <

}

cout <

}

SparseMatrix TransposeSMatrix(int* matrix){

SparseMatrix tmp(matrix,ROW,COL,0);

tmp._row = _col;

tmp._col = _row;

size_t count = 0;

for (int i = 0; i 

size_t index = 0;

for (index = 0; index 

if (_array[index]._col == i){

tmp._array[count]._row = _array[index]._col;              //transpose

tmp._array[count]._col = _array[index]._row;

tmp._array[count]._value = _array[index]._value;

count++;

}

}

}

tmp.Display();

return tmp;

}

private:

vector> _array;

size_t _row;

size_t _col;

};

void test(){

int matrix[ROW][COL] = { { 1, 0, 3, 0, 5 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 1, 0, 3, 0, 5 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } };

SparseMatrix sm((int*)matrix, ROW, COL,0);

sm.Display();

sm.TransposeSMatrix((int *)matrix);

}

main.cpp#include

using namespace std;

#include"SparseMatrix.hpp"

int main(){

test();

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值