2 つの const_iterators を作成する疎行列の実装をテストするために、main.cpp を実行しています。
SparseMatrix<double>::const_iterator a,b;
a=mata.begin(); //mata previously created as SparseMatrix<double>
b=mata.end();
... //code goes on
問題は、begin と end を呼び出さないことです (最初の cout も実行しません) が、2 つの反復子を作成すると機能します。const_iterators の begin と end をどのように実装したかを次に示します。
const_iterator begin() const
{
cout<<"Begin"<<endl;
int minr=minRow();
int minc=minCol(findRow(minr));
mcol * mc=findCol(findRow(minr),minc);
const_iterator x;
if(mc!=NULL)
{
T* dato=&(mc->data);
x= const_iterator(genElement(minr,minc,dato));
}
else
{
x=const_iterator(NULL);
}
x.setSM(const_cast<SparseMatrix<T>*>(this));
return x;
}
const_iterator end() const
{
cout<<"End"<<endl;
const_iterator x= const_iterator(NULL);
x.setSM(const_cast<SparseMatrix<T>*>(this));
return x;
}
私が気付いた奇妙なことは、SparseMatrix のクラス メソッド内に 2 つの const_iterators を作成すると、それらが機能することです。