本文在介紹關(guān)于C++實現(xiàn)稀疏矩陣的壓縮存儲、轉(zhuǎn)置、快速轉(zhuǎn)置的基礎(chǔ)上,重點探討了其具體步驟分享了代碼,本文內(nèi)容緊湊,希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、邯山網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計、商城系統(tǒng)網(wǎng)站開發(fā)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為邯山等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
/*稀疏矩陣的壓縮存儲、轉(zhuǎn)置、快速轉(zhuǎn)置*/
#include <iostream>
using namespace std;
#include <vector>
//三元組
template<class T>
struct Triple
{
size_t _row;
size_t _col;
T _value;
Triple(size_t row = 0, size_t col = 0, const T& value = T())
:_row(row)
,_col(col)
,_value(value)
{}
};
template<class T>
class SparseMatrix
{
public:
SparseMatrix(T* a = NULL, size_t M = 0, size_t N = 0, const T& invalid = T())
:_rowSize(M)
,_colSize(N)
,_invalid(invalid)
{
for (size_t i = 0; i < M; ++i)
{
for (size_t j = 0; j < N; ++j)
{
if (a[i*N+j] != _invalid)
{
Triple<T> t;
t._row = i;
t._col = j;
t._value = a[i*N+j];
_a.push_back(t);
}
}
}
}
void Display()
{
size_t index = 0;
for (size_t i = 0; i < _rowSize; ++i)
{
for (size_t j = 0; j < _colSize; ++j)
{
if (index < _a.size()
&& (_a[index]._row == i)
&& (_a[index]._col == j))
{
cout<<_a[index++]._value<<" ";
}
else
{
cout<<_invalid<<" ";
}
}
cout<<endl;
}
}
//矩陣轉(zhuǎn)置 時間復(fù)雜度為 O(有效數(shù)據(jù)的個數(shù)*原矩陣的列數(shù))
SparseMatrix<T> Transport()
{
SparseMatrix<T> sm;
sm._colSize = _rowSize;
sm._rowSize = _colSize;
sm._invalid = _invalid;
for (size_t i = 0; i < _colSize; ++i)
{
size_t index = 0;
while (index < _a.size())
{
if (_a[index]._col == i)
{
Triple<T> t;
t._row = _a[index]._col;
t._col = _a[index]._row;
t._value = _a[index]._value;
sm._a.push_back(t);
}
++index;
}
}
return sm;
}
//快速轉(zhuǎn)置 時間復(fù)雜度為O(有效數(shù)據(jù)的個數(shù)+原矩陣的列數(shù))
SparseMatrix<T> FastTransport()
{
SparseMatrix<T> sm;
sm._rowSize = _colSize;
sm._colSize = _rowSize;
sm._invalid = _invalid;
int* RowCounts = new int[_colSize];
int* RowStart = new int [_colSize];
memset(RowCounts, 0, sizeof(int)*_colSize);
memset(RowStart, 0, sizeof(int)*_colSize);
size_t index = 0;
while (index < _a.size())
{
++RowCounts[_a[index]._col];
++index;
}
for (size_t i = 1; i < _colSize; ++i)
{
RowStart[i] = RowStart[i-1] + RowCounts[i-1];
}
index = 0;
sm._a.resize(_a.size());
while (index < sm._a.size())
{
Triple<T> t;
t._row = _a[index]._col;
t._col = _a[index]._row;
t._value = _a[index]._value;
sm._a[RowStart[_a[index]._col]] = t;
++RowStart[_a[index]._col];
++index;
}
delete[] RowCounts;
delete[] RowStart;
return sm;
}
protected:
vector<Triple<T>> _a;
size_t _rowSize;
size_t _colSize;
T _invalid;
};
void Test()
{
int array [6][5] =
{
{1, 0, 3, 0, 5},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{2, 0, 4, 0, 6},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}
};
SparseMatrix<int> sm1((int*)array, 6, 5, 0);
sm1.Display();
cout<<endl;
//SprseMatrix<int> sm2 = sm1.Transport();
SparseMatrix<int> sm2 = sm1.FastTransport();
sm2.Display();
}
int main()
{
Test();
return 0;
}
看完上訴內(nèi)容,你們掌握C++實現(xiàn)稀疏矩陣的壓縮存儲、轉(zhuǎn)置、快速轉(zhuǎn)置的方法了嗎?如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前名稱:C++實現(xiàn)稀疏矩陣的壓縮存儲、轉(zhuǎn)置、快速轉(zhuǎn)置代碼分享
標(biāo)題來源:http://www.chinadenli.net/article46/peegeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站改版、App設(shè)計、外貿(mào)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)