【问题标题】:C++, boost::numeric::ublas::mapped_matrix - iterating problem when using std::tr1::unordered_map instead of std::mapC++,boost::numeric::ublas::mapped_matrix - 使用 std::tr1::unordered_map 而不是 std::map 时的迭代问题
【发布时间】:2011-02-11 10:29:49
【问题描述】:

我正在使用 boost 库 (1.44) 和 VC++ 2010。

我发现下面的代码有问题,

using namespace boost::numeric;
typedef double value_type;

typedef ublas::mapped_matrix<value_type> StorageMap;
typedef ublas::mapped_matrix<value_type, ublas::row_major, std::tr1::unordered_map<size_t, value_type> > StorageUnorderedMap;

StorageMap mat; //<== (1) 
//StorageUnorderedMap mat; //<== (2)

//Looping over non-zero elements of sparse matrix.
size_t numElemLoop= 0;
for(auto it1= mat.begin1(); it1 != mat.end1(); ++it1)
{
    for(auto it2= it1.begin(); it2 != it1.end(); ++it2)
    ++numElemLoop;
}

assert(mat.nnz() == numElemLoop); //<== (3)

此测试仅针对使用 std::tr1::unordered_map 的 StorageUnorderedMap 失败。 但是 insert_element() 和 find_element() 测试全部通过。

【问题讨论】:

  • 无法用您的代码来判断。你初始化一个空的StorageMap 并循环它。

标签: c++ boost ublas


【解决方案1】:

也许尝试使用 unordered_multimap。由于相同的键,某些插入可能会失败。那么计数将不匹配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2010-09-07
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多