Reputation: 3484
we need a container that keeps its elements sorted by the element's priority when appending a new element, that has the ability to retrieve an element given it's id.
(the problem with priority queue is that it doesn't give you the ability to retrieve an element according to id and not priority)
thanks
Upvotes: 1
Views: 342
Reputation: 14392
boost multi index containers give you the ability to have a sorted view on priority and a sorted view on ID.
A small example:
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <iostream>
#include <vector>
#include <cstddef>
#include <iterator>
struct elem {
std::size_t id;
int priority;
};
int main()
{
using namespace boost::multi_index;
typedef multi_index_container<
elem,
indexed_by<
ordered_unique<member<elem,std::size_t,&elem::id> >,
ordered_non_unique<member<elem,int,&elem::priority> >
>
> elem_container;
// just for show
std::vector<elem> elems =
{{0, 25},
{1, 10},
{2, 100},
{3, 6}
};
elem_container elemc(begin(elems), end(elems));
// by id
std::cout << "By ID: " << std::endl;
for(auto& x : elemc.get<0>()) {
std::cout << "id: " << x.id << "priority: " << x.priority << std::endl;
}
// by priority
std::cout << "By Priority: " << std::endl;
for(auto& x : elemc.get<1>()) {
std::cout << "id: " << x.id << "priority: " << x.priority << std::endl;
}
return 0;
}
Upvotes: 7