【问题标题】:Is thare in STL or BOOST map like container with find and pop operation?STL 或 BOOST 地图中是否有类似容器的查找和弹出操作?
【发布时间】:2011-12-04 00:11:30
【问题描述】:

我希望我的地图是可搜索的,并且我希望能够从其中踢出很久以前插入其中的元素(使用像 map.remove(map.get_iterator_to_oldest_inserted_element()) 这样的 api),比如 queqe 和 map 的混合......有没有这样的STL 或 Boost 中的容器?

【问题讨论】:

    标签: c++ boost stl map std


    【解决方案1】:

    您可以设置boost multi-index container 来执行此操作。

    但是,我无法理解多索引容器。我认为创建我自己的班级会更容易,该班级的成员有 std::queuestd::map,并自己管理。

    【讨论】:

    • 这会不必要地复制数据——MultiIndex 不会复制数据,但您是否指定了非侵入式访问器。
    【解决方案2】:

    在 boost 和 stl 中没有类似的东西,但你可以自己制作一个混合的:

    map<Key, Value> Map;
    deque<Key> Queue;
    
    void insert(const Key &k, const Value &v)
    {
        Map[k] = v;
        Queue.push_back(k);
    }
    void pop_front()
    {
        const Key &k = Queue.front();
        Map.erase(k);
        Queue.pop_front();
    }
    

    【讨论】:

    • 所以我试着听从你的建议.. 但我发现按键擦除元素有一个小问题......我描述了它here
    【解决方案3】:

    Boost::bimap 可用于使用基于列表的关系集创建单向映射。

    typedef bimap<set_of<A>, unconstrained_set_of<B>, list_of_relation> custom_map_type;
    
    custom_map_type map;
    
    map.push_back(custom_map_type::value_type(A(), B()));
    
    // delete the front of the list (the first inserted element if only using push_back)
    map.pop_front(); 
    
    // otherwise, set.right behave a lot like a std::map<A,B>, with minor changes.
    

    【讨论】:

      【解决方案4】:

      如果您只想找到最旧的(或最新的——或者更一般地说,根据某些指定的标准,是最小/最大的,并且能够删除该元素),那么 priority_queue 将完成这项工作。

      【讨论】:

        【解决方案5】:

        您可以使用ordered_uniquesequence 索引来使用boost::multi_index,如本例所示。

        #include <iostream>
        #include <boost/multi_index_container.hpp>
        #include <boost/multi_index/ordered_index.hpp>
        #include <boost/multi_index/sequenced_index.hpp>
        #include <boost/multi_index/member.hpp>
        
        // Element type to store in container
        struct Element
        {
            std::string key;
            int value;
        
            Element(const std::string& key, int value) : key(key), value(value) {}
        };
        
        namespace bmi = boost::multi_index;
        
        // Boost multi_index container
        typedef bmi::multi_index_container<
            Element,
            bmi::indexed_by<
                bmi::ordered_unique< bmi::member<Element,std::string,&Element::key> >,
                bmi::sequenced<> >
            >
            MyContainer;
        
        typedef MyContainer::nth_index<1>::type BySequence;
        
        // Helper function that returns a sequence view of the container.
        BySequence& bySequence(MyContainer& container) {return container.get<1>();}
        
        int main()
        {
            MyContainer container;
        
            // Access container by sequence. Push back elements.
            BySequence& sequence = bySequence(container);
            sequence.push_back(Element("one", 1));
            sequence.push_back(Element("two", 2));
            sequence.push_back(Element("three", 3));
        
            // Access container by key. Find an element.
            // By default the container is accessed as nth_index<0>
            MyContainer::const_iterator it = container.find("two");
            if (it != container.end())
                std::cout << it->value << "\n";
        
            // Access container by sequence. Pop elements in a FIFO manner,
            while (!sequence.empty())
            {
                std::cout << sequence.front().value << "\n";
                sequence.pop_front();
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-11
          • 2011-05-12
          • 1970-01-01
          相关资源
          最近更新 更多