如果优先级和键是不同的属性,您可以使用 2-index multi_index_container。快速而肮脏的例子如下:
Live Coliru Demo
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
using namespace boost::multi_index;
struct element
{
int key;
int area51;
};
using x_files_base=multi_index_container<
element,
indexed_by<
ordered_non_unique<member<element,int,&element::area51>>,
hashed_unique<member<element,int,&element::key>>
>
>;
class x_files:x_files_base
{
public:
using x_files_base::iterator;
using x_files_base::const_iterator;
using x_files_base::x_files_base;
using x_files_base::begin;
using x_files_base::end;
void pop(){erase(begin());}
bool push(const element& x){return insert(x).second;}
bool check(int key){return project<0>(get<1>().find(key))!=end();}
void update(int key,int area51)
{
auto it=project<0>(get<1>().find(key));
if(it!=end())modify(it,[=](element& x){x.area51=area51;});
}
};
#include <iostream>
#include <string>
void dump(const x_files& xf)
{
std::string delim="";
for(const element& x:xf){
std::cout<<delim<<"["<<x.key<<","<<x.area51<<"]";
delim=",";
}
std::cout<<"\n";
}
int main()
{
x_files xf={{100,0},{80,1},{90,2},{95,3}};
dump(xf);
xf.pop();
dump(xf);
xf.push({70,4});
dump(xf);
std::cout<<(xf.check(70)?"true":"false")<<"\n";
xf.update(70,0);
dump(xf);
}
输出
[100,0],[80,1],[90,2],[95,3]
[80,1],[90,2],[95,3]
[80,1],[90,2],[95,3],[70,4]
真的
[70,0],[80,1],[90,2],[95,3]