0

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 を作成すると、それらが機能することです。

4

1 に答える 1

2

あなたが言うように、「mata[ sic ] 以前は として作成されましたSparseMatrix<double>」が、あなたのbeginendあなたが示すものは とマークされていますconst。これらのconstメンバー関数を呼び出すには、オブジェクトmetaが である必要がありますconst。そうでない場合は、非constバージョンのbeginandendが呼び出されます。

于 2012-02-08T21:07:07.077 に答える