65,202
社区成员




int main()
{
Eigen::SparseMatrix<double> SparseMatrix_Debugger(5, 5);
vector<double> vec_Debugger;
TripleList TripleList_Debugger;
vector<Eigen::Triplet<double>> Triplet_Debugger;
double Row[11] = { 0, 0, 0, 1, 1, 2, 2, 2, 3, 4, 4 };
double Col[11] = { 0, 2, 3, 0, 4, 1, 2, 4, 1, 0, 2 };
double Val[11] = { 3, 8, 4, 1, 6, 2, 2, 8, 2, 6, 7 };
for (int i = 0;i <= 10;i++)
{
// 将三元表数据存储在自编的 TripleList 类中
TripleList_Debugger.Row.push_back(Row[i]);
TripleList_Debugger.Col.push_back(Col[i]);
TripleList_Debugger.Value.push_back(Val[i]);
//下面的这个 CombineTripleList() 是我自己编的 void 型函数,目的是把 TripleList 类中的 Row、Col、Value 分别逐次赋值给一个 vector 以便于用 swap 函数给 Triplet.Debugger 这个 vector 赋值
TripleList_Debugger.CombineTripleList();
//此处直接用 swap 赋值会报错,这个小弟也不明白是为什么
//Triplet_Debugger.swap(TripleList_Debugger.BridgeToTriplet);
Triplet_Debugger.emplace_back(TripleList_Debugger.Row[i], TripleList_Debugger.Col[i], TripleList_Debugger.Value[i]);
}
SparseMatrix_Debugger.setFromTriplets(Triplet_Debugger.begin(), Triplet_Debugger.end());
Eigen::MatrixXd MatrixXd_Debugger = SparseMatrix_Debugger;
//下面是输出矩阵测试结果
cout << "MatrixXd:\n" << MatrixXd_Debugger << endl;
cout << "\nRow:" << endl;
for (int a = 0;a < TripleList_Debugger.Row.size();a++)
{
cout << TripleList_Debugger.Row[a] << endl;
}
cout << "\nCol:" << endl;
for (int b = 0;b < TripleList_Debugger.Col.size();b++)
{
cout << TripleList_Debugger.Col[b] << endl;
}
cout << "\nValue:" << endl;
for (int c = 0;c < TripleList_Debugger.Value.size();c++)
{
cout << TripleList_Debugger.Value[c] << endl;
}
cout << "\nBridgeToTrplet\n:" << endl;
for (int t = 0;t < TripleList_Debugger.BridgeToTriplet.size();t++)
{
cout << TripleList_Debugger.BridgeToTriplet[t] << endl;
}
return 0;
}
//TripleList.h
class TripleList
{
public:
TripleList();
~TripleList();
std::vector <double> Row;
std::vector <double> Col;
std::vector <double> Value;
std::vector <double> BridgeToTriplet;
double FindValue(double row, double col);
void CombineTripleList();
};
//TripleList.cpp
#include "stdafx.h"
#include "TripleList.h"
TripleList::TripleList()
{
}
TripleList::~TripleList()
{
}
double TripleList::FindValue(double row, double col)
{
double Ans;
if (row <= 0 || col <= 0)
AfxMessageBox(_T("The coordinate you've entered is illegal, please cheak and reenter!"));
else
for (int i = 0; Row.at(i) == row; i++)
{
for (int n = i; Col.at(n) == col; n++)
{
Ans = Value.at(n);
}
}
return Ans;
}
void TripleList::CombineTripleList()
{
for (int i = 0;i < Row.size();i++)
BridgeToTriplet.emplace_back(Row.at(i), Col.at(i), Value.at(i));
}
double Row[11] = { 0., 0., 0., 1., 1., 2., 2., 2., 3., 4., 4. };
double Col[11] = { 0., 2., 3., 0., 4., 1., 2., 4., 1., 0., 2. };
double Val[11] = { 3., 8., 4., 1., 6., 2., 2., 8., 2., 6., 7. };
试试看。