代码之家  ›  专栏  ›  技术社区  ›  ossandcad

VisualC++ + HASHMultIMAP没有发现任何结果

  •  1
  • ossandcad  · 技术社区  · 14 年前

    我需要一些帮助来理解stdext::hash\u multimap的下限、上限和相等范围是如何工作的(至少是VS2005版本)。

    我有以下代码(问题摘要)

    #include <hash_map>
    
    using stdext::hash_multimap;
    using std::greater;
    using stdext::hash_compare;
    using std::pair;
    using std::cout;
    
    typedef hash_multimap < double, CComBSTR, hash_compare< double, greater<double> > > HMM;
    HMM hm1;
    HMM :: const_iterator it1, it2;
    pair<HMM::const_iterator, HMM::const_iterator> pairHMM;
    
    typedef pair <double, CComBSTR> PairDblStr;
    
    // inserting only two values for sample
    hm1.insert ( PairDblStr ( 0.224015748, L"#1-64" ) );
    hm1.insert ( PairDblStr ( 0.215354331, L"#1-72" ) );
    
    // Using a double value in between the inserted key values to find one of the elements in the map
    it1 = hm1.lower_bound( 0.2175 );
    
    if( it1 == hm1.end() )
    {
        cout << "lower_bound failed\n";
    }
    
    it1 = hm1.upper_bound( 0.2175 );
    
    if( it1 == hm1.end() )
    {
        cout << "upper_bound failed\n";
    }
    
    pairHMM = hm1.equal_range( 0.2175 );
    if( ( pairHMM.first == hm1.end() ) && ( pairHMM.second == hm1.end() ) )
    {
        cout << "equal_range failed\n";
    }
    

    lower_bound failed
    upper_bound failed
    equal_range failed
    

    提前谢谢你的帮助。

    感谢@billy oneal@dauphic的评论和编辑。我已经更新了上面的代码,使其可编译和可运行(当然,一旦包含了正确的头)。

    1 回复  |  直到 14 年前
        1
  •  3
  •   James McNellis    14 年前

    我们能不能用这些方法找到一个“最接近的匹配”键?

    hash_multimap 使用哈希表实现。两个彼此非常接近的键(例如0.2153和0.2175)很可能映射到哈希表中完全不同的bin。

    这个 lower_bound , upper_bound ,和 equal_range 哈希多重映射 在VisualC++标准库扩展中有点奇怪的实现。

    the documentation for lower_bound :

    X 在受控序列中,散列到与key相同的bucket,并与key具有等价的顺序。如果不存在这样的元素,则返回 hash_map::end ; 否则它返回一个迭代器 . 您可以使用它来定位当前在受控序列中与指定键匹配的元素序列的开头。

    以及 the documentation for upper_bound :

    成员函数决定最后一个元素 在受控序列中,散列到与key相同的bucket,并与key具有等价的顺序。如果不存在这样的元素,或者 哈希映射::结束 ; 否则,它将返回一个迭代器,指定第一个元素 . 您可以使用它来定位当前在受控序列中与指定键匹配的元素序列的结尾。

    与…的行为相同 std::lower_bound std::map::lower_bound

    对于它的价值,C++ 0x无序关联容器不提供 , 上限 功能。

    你对我的要求有什么建议吗?

    是的:如果你需要 下界 上限 std::multimap .