STL或BOOST映射中是否有类似容器的查找和弹出操作?

发布于 2024-12-08 19:13:30 字数 160 浏览 1 评论 0原文

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

I want my map to be searchable and I want to be capable to kick out from it elements that were inserted into it longest time ago (with api like map.remove(map.get_iterator_to_oldest_inserted_element()) ) like mix of queqe and map.. Is there any such container in STL or Boost?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泡沫很甜 2024-12-15 19:13:30

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

#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();
    }
}

You can use boost::multi_index using the ordered_unique and sequence indices, as in this example.

#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();
    }
}
南街九尾狐 2024-12-15 19:13:30

您可以设置boost 多索引容器 来执行此操作。

但是,我很难理解多索引容器。我认为推出我自己的具有 std::queue 和 std::map 作为成员的类并自己管理会更容易。

You can set up a boost multi-index container to do this.

However, I have trouble understanding multi-index containers. I think it would be easier to roll my own class that has as members a std::queue and a std::map, and manage it myself.

不必你懂 2024-12-15 19:13:30

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();
}

There is nothing like that in boost and stl, but you can make a hybrid one yourself:

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();
}
可是我不能没有你 2024-12-15 19:13:30

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

If you only ever want to find the oldest (or newest -- or more generally the least/greatest by some specified criteria, and be able to remove that element), then a priority_queue will do the job.

○愚か者の日 2024-12-15 19:13:30

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.

Boost::bimap can be used to create a unidirectionnal map using a list-based relation set.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文