是thare在STL或增大图像查找并弹出操作容器? [英] Is thare in STL or BOOST map like container with find and pop operation?
本文介绍了是thare在STL或增大图像查找并弹出操作容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的地图,可搜索,我想能够从该像被时间最长的前插入它(与它的API元素踢出 map.remove(map.get_iterator_to_oldest_inserted_element())
),如queqe和地图的搭配..是否有STL中的任何此类容器或提升?
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?
推荐答案
您可以使用的的boost :: multi_index 使用 ordered_unique
和序
指标,如这个例子。
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();
}
}
这篇关于是thare在STL或增大图像查找并弹出操作容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文