# 对称矩阵、稀疏矩阵及矩阵的逆置与加法

矩阵之对称矩阵、稀疏矩阵逆置与加法

#include<iostream>
using namespace std;
#include<vector>

//对称矩阵
template<class T,size_t N=5>
class SymmetricMatrix
{
public:
SymmetricMatrix(T(&arr)[N][N])
:_N(N)
{
int index = 0;
_arr.resize((1 + N)*N >> 1);
for (size_t i = 0; i < _N; i++)
{
for (size_t j = 0; j <= i; j++)
{
_arr[index++] = arr[i][j];
}
}
}
T& Acess(int row, int col)
{
if (row < col)
swap(row, col);
return _arr[row*(row + 1) / 2 + col];
}
void Print()
{
for (size_t i = 0; i < N; i++)
{
for (size_t j = 0; j < N; j++)
{
cout << Acess(i, j) << " ";
}
cout << endl;
}
cout << endl;
}
private:
vector<T> _arr;
size_t _N;
};

int main()
{
int a[5][5] = {
{ 0, 1, 2, 3, 4 },
{ 1, 0, 1, 2, 3 },
{ 2, 1, 0, 1, 2 },
{ 3, 2, 1, 0, 1 },
{ 4, 3, 2, 1, 0 } };
SymmetricMatrix<int> s(a);
cout << s.Acess(2, 3) << endl;
cout << s.Acess(3, 2) << endl;

s.Acess(3, 2);
s.Print();

system("pause");
return 0;
}

1、稀疏矩阵的压缩存储和还原

{row，col，value}三元组（Trituple）存储每一个有效数据，三元组按原矩阵中的位置，以行优先级先后顺序依次存放。

template<class T>
struct Trituple
{
size_t _row;
size_t _col;
T _value;

Trituple(int row,int col,int data)
:_row(row)
, _col(col)
, _value(data)
{}
};

template<class T,int M,int N>
class SparseMatrix
{
public:
SparseMatrix(int row,int col,T inValid)
:_row(row)
, _col(col)
, _inValid(inValid)
{}
SparseMatrix(T arr[M][N], T inValid)                 //存储稀疏矩阵
:_row(M)
, _col(N)
, _inValid(inValid)
{
for (size_t i = 0; i < _row; i++)
{
for (size_t j = 0; j < _col; j++)
{
if (arr[i][j] != inValid)
{
Trituple<T> t(i, j, arr[i][j]);
_v.push_back(t);
}
}
}
}
//打印压缩矩阵
void display()
{
for (size_t i = 0; i < _v.size(); i++)
{
cout << _v[i]._value << " ";
}
}
//打印
void Print()
{
size_t index = 0;
for (size_t i = 0; i < _row; i++)
{
for (size_t j = 0; j < _col; j++)
{
if (index < _v.size())
{
if (_v[index]._row == i&&_v[index]._col == j)
{
cout << _v[index++]._value << " ";
}
else
{
cout << _inValid << " ";
}
}
else
cout << _inValid << " ";
}
cout << endl;
}
cout << endl;
}
//重载输出运算符
friend ostream& operator<<(ostream& cout, SparseMatrix<T, M, N>& Matrix)
{
size_t index = 0;
for (size_t i = 0; i < Matrix._row; i++)
{
for (size_t j = 0; j < Matrix._col; j++)
{
if (index < Matrix._v.size())
{
if (Matrix._v[index]._row == i&&Matrix._v[index]._col == j)
{
cout << Matrix._v[index++]._value << " ";
}
else
{
cout << Matrix._inValid << " ";
}
}
else
cout << Matrix._inValid << " ";
}
cout << endl;
}
return cout;
}
private:
vector<Trituple<T>> _v;        //保存有效节点
size_t _row;
size_t _col;
T _inValid;                   //无效值
};

int main()
{
int array[5][5] = {
{ 0, 0, 1, 0, 2 },
{ 1, 0, 0, 0, 8 },
{ 0, 0, 0, 1, 3 },
{ 1, 0, 0, 0, 0 },
{ 0, 0, 0, 3, 0 } };

SparseMatrix<int,5,5> sm(array, 0);
sm.display();

sm.Print();
cout << sm << endl;
system("pause");
return 0;
}

//矩阵逆置
void ReverseMatrix()
{
for (size_t index = 0; index < _v.size();index++)
{
swap(_v[index]._row, _v[index]._col);
}
swap(_row, _col);
}

//矩阵逆置
void ReverseMatrix()
{
for (size_t index = 0; index < _v.size();index++)
{
swap(_v[index]._row, _v[index]._col);
}
swap(_row, _col);

for (size_t i = 0; i < _v.size(); i++)
{
for (size_t j = 0; j < _v.size() - i - 1; j++)
{
if (_v[j]._row>_v[j + 1]._row)
swap(_v[j], _v[j + 1]);
}
}
}

SparseMatrix& operator+(SparseMatrix& Matrix)
{
//行列分别相等
if (_row != Matrix._row || _col != Matrix._col)
{
cout << "两个矩阵不能相加" << endl;
exit(EXIT_FAILURE);
}

SparseMatrix* ret = new SparseMatrix(_row, _col, _inValid);
size_t leftIndex = 0, rightIndex = 0;
while (leftIndex < _v.size() && rightIndex < Matrix._v.size())
{
//在原矩阵中的偏移量
{
ret->_v.push_back(_v[leftIndex]);
leftIndex++;
}
{
ret->_v.push_back(Matrix._v[rightIndex]);
rightIndex++;
}
else
{
Trituple<T> t(_v[leftIndex]._row, _v[leftIndex]._col,
_v[leftIndex]._value + Matrix._v[rightIndex]._value);
ret->_v.push_back(t);
leftIndex++;
rightIndex++;
}
}
while (leftIndex < _v.size())   //第二个已经无有效元素
{
ret->_v.push_back(_v[leftIndex]);
leftIndex++;
}
while (rightIndex < _v.size())   //第一个已经无有效元素
{
ret->_v.push_back(Matrix._v[rightIndex]);
rightIndex++;
}
return *ret;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120