【问题标题】:Why isn't there boost::intrusive::map?为什么没有 boost::intrusive::map?
【发布时间】:2014-07-01 06:05:33
【问题描述】:

boost 文档 (http://www.boost.org/doc/libs/1_55_0/doc/html/intrusive.html) 指出,侵入式容器是为 list(单/双链接)、setmultiset 实现的。 我找不到地图的实现。有没有更深层次的原因,还是只是等待实施?

【问题讨论】:

    标签: c++ boost map intrusive-containers


    【解决方案1】:

    这是因为 map<Key, Value> 实际上是 set<std::pair<Key const, Value>> 并且 Boost.Intrusive 和 Boost.MultiIndex 集允许您使用值成员之一作为键。换句话说,如果find 可以接受一个可比较的键,而不是整个值来搜索which has been a long-standing unresolved issue with std::map and std::set,则不需要map

    关联容器查找函数(find、lower_bound、upper_bound、 equal_range) 只取 key_type 的参数,需要用户自己构造 (隐式或显式)key_type 的对象来进行查找。 这可能很昂贵,例如构造一个要在集合中搜索的大对象 当比较器函数只查看对象的一个​​字段时。那里 用户强烈希望能够使用其他类型进行搜索 与 key_type 相当。

    【讨论】:

    • 在没有值的情况下调用 find 的选项是我想首先使用 map 的原因,谢谢你的信息。
    • @kovarex 为此我推荐boost::multi_index,基本上是set 和正确的find 接口。
    • @MaximYegorushkin。在您的boost::intrusive::set<std::pair<Key const, Value>> 示例中,boost::intrusive::hook 会在哪里?它可以包含在Value 中,还是必须将std::pair<Key const, Value> 包装在一个也有钩子的对象中?
    • @MaximYegorushkin 另外,boost::intrusive::set<std::pair<Key const, Value>> 是否会持有对集合的弱引用而不是ValueKey?所以std::pair<Key const, Value> 对象需要在其他地方保持活动状态?
    • @Clivest 您可能想了解侵入式集合的工作原理和what element requirements are
    【解决方案2】:

    version 1.59 Boost.Intrusive supports map-like associative containers.它比标准的类似地图的容器灵活得多,因为程序员不需要定义任何类似std::pair 的结构来定义key_typemapped_type。一个新选项key_of_value 被添加到侵入集接口,定义值的哪一部分应被视为key_type 以及如何获取它。这样做是为了在使用 Boost.Intrusive 时简化类似地图的使用。来自 Boost 文档的示例代码:

    #include <boost/static_assert.hpp>
    #include <boost/type_traits/is_same.hpp>
    #include <boost/intrusive/set.hpp>
    #include <boost/intrusive/unordered_set.hpp>
    #include <vector>
    #include <cassert>
    
    using namespace boost::intrusive;
    
    class MyClass : public set_base_hook<>
                  , public unordered_set_base_hook<>
    {
       public:
       int first;
       explicit MyClass(int i) : first(i){}
    };
    
    //key_of_value function object, must:
    //- be default constructible (and lightweight)
    //- define the key type using "type"
    //- define an operator() taking "const value_type&" and
    //    returning "const type &"
    struct first_int_is_key
    {
       typedef int type;
    
       const type & operator()(const MyClass& v) const
       {  return v.first;  }
    };
    
    //Define omap like ordered and unordered classes 
    typedef set< MyClass, key_of_value<first_int_is_key> > OrderedMap;
    typedef unordered_set< MyClass, key_of_value<first_int_is_key> > UnorderedMap;
    
    int main()
    {
       BOOST_STATIC_ASSERT((boost::is_same<  OrderedMap::key_type, int>::value));
       BOOST_STATIC_ASSERT((boost::is_same<UnorderedMap::key_type, int>::value));
    
       //Create several MyClass objects, each one with a different value
       //and insert them into the omap
       std::vector<MyClass> values;
       for(int i = 0; i < 100; ++i)  values.push_back(MyClass(i));
    
       //Create ordered/unordered maps and insert values
       OrderedMap   omap(values.begin(), values.end());
       UnorderedMap::bucket_type buckets[100];
       UnorderedMap umap(values.begin(), values.end(), UnorderedMap::bucket_traits(buckets, 100));
    
       //Test each element using the key_type (int)
       for(int i = 0; i != 100; ++i){
          assert(omap.find(i) != omap.end());
          assert(umap.find(i) != umap.end());
          assert(omap.lower_bound(i) != omap.end());
          assert(++omap.lower_bound(i) == omap.upper_bound(i));
          assert(omap.equal_range(i).first != omap.equal_range(i).second);
          assert(umap.equal_range(i).first != umap.equal_range(i).second);
       }
    
       //Count and erase by key
       for(int i = 0; i != 100; ++i){
          assert(1 == omap.count(i));
          assert(1 == umap.count(i));
          assert(1 == omap.erase(i));
          assert(1 == umap.erase(i));
       }
       assert(omap.empty());
       assert(umap.empty());
    
       return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      相关资源
      最近更新 更多