#include <iostream>
#include <vector>
using namespace std;
template<class T>
struct Triple //三元组
{
size_t _row; //行
size_t _col; //列
T _value; //值
Triple(size_t row, size_t col, const T& value)
:_row(row)
, _col(col)
, _value(value)
{}
};
template<class T>
class SparseMatrix //稀疏矩阵
{
protected:
vector<Triple<T>> _matrix; //可以实现动态增容的压缩矩阵
size_t _m; //行
size_t _n; //列
T _invalid; //默认值
public:
SparseMatrix(T* a, size_t m, size_t n, const T& invalid= T())
:_m(m)
, _n(n)
, _invalid(invalid)
{
for (size_t i = 0; i < m; ++i)
{
for (size_t j = 0; j < n; ++j)
{
Triple<T> t(i, j, a[i*n + j]);
_matrix.push_back(t);
}
}
}
void Display()
{
size_t index = 0;
for (size_t i = 0; i < _m; ++i)
{
for (size_t j = 0; j < _n; ++j)
{
if (index < _matrix.size()
&& _matrix[index]._row== i
&&_matrix[index]._col ==j)
{
cout << _matrix[index]._value << " ";
++index;
}
else
{
cout << _invalid << " ";
}
}
cout << endl;
}
cout << endl;
}
};
#include <windows.h>
void test()
{
int a[6][5] =
{
{ 1, 0, 2, 0, 0 },
{ 1, 0, 1, 0, 3 },
{ 2, 0, 0, 1, 2 },
{ 3, 0, 1, 0, 0 },
{ 4, 0, 2, 0, 0 },
{ 0, 3, 4, 0, 0 },
};
SparseMatrix<int> sm((int*)a, 6, 5, 0);
//SymmetricMatrix(int a[][N], size_t N)
sm.Display();
}
int main()
{
test();
system("pause");
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有