我不知道Boost是否會爲你工作,但我會檢查Boost Mutli索引。 http://www.boost.org/doc/libs/1_53_0/libs/multi_index/doc/index.html
您可以保持索引的優先級,以便您快速獲取並插入元素。我已經同樣使用增強多指標進行MRU/LRU情況。
#include <iostream>
#include <string>
#include <algorithm>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/optional.hpp>
struct bypriority{};
struct byseq{};
struct MyElement{
typedef int priority_type;
typedef std::string name_type;
name_type name;
priority_type priority;
MyElement(const name_type& name, const priority_type& priority):name(name),priority(priority){};
};
std::ostream& operator<<(std::ostream& os, const MyElement& e){
os << "Name: " << e.name << " Priority: " << e.priority;
return os;
}
using namespace boost;
using namespace boost::multi_index;
template<typename Element>
struct Container{
typedef multi_index_container<
Element,
indexed_by<
//sequenced
sequenced<tag<byseq> >,
//ordered by priority
ordered_non_unique<tag<bypriority>,member<Element,typename Element::priority_type,&Element::priority>, std::greater<typename Element::priority_type> >
>
> Elements;
void insert(const Element& e){
typename Elements::template index<byseq>::type& list_view = elements.get<byseq>();
list_view.push_back(e);
}
boost::optional<Element> peek() const{
boost::optional<Element> res;
typename Elements::template index<bypriority>::type::iterator it = elements.get<bypriority>().begin();
if(it != elements.get<bypriority>().end()){
res.reset(*it);
}
return res;
}
boost::optional<Element> pop() {
boost::optional<Element> res;
typename Elements::template index<bypriority>::type& priority_index = elements.get<bypriority>();
typename Elements::template index<bypriority>::type::iterator it = elements.get<bypriority>().begin();
if(it != elements.get<bypriority>().end()){
res.reset(*it);
priority_index.erase(it);
}
return res;
}
void print_in_order(std::ostream& os) const{
typedef typename Elements::template index<byseq>::type elements_by_sequence;
for(typename elements_by_sequence::iterator it = elements.get<0>().begin(); it != elements.get<0>().end(); it++){
os << *it << std::endl;
}
}
protected:
Elements elements;
};
using namespace std;
int main(int argc, char *argv[]) {
Container<MyElement> c;
c.insert(MyElement("Bob",10));
c.insert(MyElement("Alice",100));
c.insert(MyElement("Fred",20));
c.print_in_order(std::cout);
cout << endl << "Highest Priority is " << endl << *c.peek() << endl;
boost::optional<MyElement> alice = c.pop();
if(alice){
cout << "Popped results are " << *alice << endl;
}
cout << endl << "Now the contents are" << endl;
c.print_in_order(std::cout);
}
將輸出:
Name: Bob Priority: 10
Name: Alice Priority: 100
Name: Fred Priority: 20
Highest Priority is
Name: Alice Priority: 100
Popped results are Name: Alice Priority: 100
Now the contents are
Name: Bob Priority: 10
Name: Fred Priority: 20
可能是[Treap](http://en.wikipedia.org/wiki/Treap)數據結構? – 2013-02-28 16:02:00
@Evgeny呃,這個treap依賴於用於堆結構的優先級值的隨機性。 OP要求他自己的優先值。 – us2012 2013-02-28 16:03:49
聽起來像[Boost bimap](http://www.boost.org/doc/libs/1_53_0/libs/bimap/doc/html/index.html)應該很容易地完成這項工作。如果您可能需要兩個以上的關鍵字段,Boost multi_index可以做到這一點,但只要您只需要兩個,bimap可能會更簡單。 – 2013-02-28 16:04:56