0

以下のコード (ほぼ同じ) が本番環境にあり、奇妙な動作が見られます。「HERE」とマークされたセクションは、accrualRows ディクショナリに最後に挿入されたものを常に出力します。「Row」へのポインターを格納するように hash_map を変更すると、すべて正常に動作します。std コンテナーで const & を使用することに少し疑いがあります。標準コンテナーでは参照を使用できませんが、これらは定数参照であり、一部の場所では異なる扱いになることがわかっています (例: 一時変数を割り当てることができます)。および定数参照へのリテラル)

#define hash_map std::tr1::unordered_map
//build up a hash map between deal index and the row for ones we care about

    typedef hash_map<int, const Row &> RowMap;
    RowMap accrualRows;    
    for( int i = 0; i < listItems.numResults(); ++i )
    {
        const Row & accrual = listItems.getResult(i);
        if( accrual.someVar )
        {
            accrualRows.insert( std::make_pair( accrual.index, accrual ) );
         }
    }

    //go through every row and if deal index is in our accrualRows, then
    //modify 

    for( int i = 0; i < numResults(); ++i )
    {
        ExposureRow & exposure = getResult(i);
        RowMap::const_iterator it = accrualRows.find( exposure.index );

        if( it != accrualRows.end() )
        {
            // HERE
            cout << it->second.someVar << endl;
        }
    }
}

誰が問題が何であるかを見ることができますか?

4

1 に答える 1

1

コンテナに参照を格納することはできません。コンテナはオブジェクトのみを格納できます。「参照」を格納する必要がある場合はstd::reference_wrapper、ポインタを使用するか使用できます。

于 2012-07-19T07:01:25.260 に答える