c++11 - creation of priority queue - c++ -
i want create general priority queue. yes know can achieved stl still don't have full understand of , why want build 1 myself. when general mean in sense act same int,string,any class,etc. understand each item in queue needs have @ least 2 fields : 1. value 2. priority figure value field should template found out there ain't such option. bit lost here should locate template , how use afterward.
edit1: 1st 2 comments said question broad i'll try narrow down. lets got start of pq:
template <class t> class stack { public: int priority template <t> value stack(s); //ctor }
this code how fought should written doesn't compile. if had methods in class need write before each implementation of each method :
template<class t>
or maybe can use :
stack<t>::stack(int s) //ctor
if @ std::priority_queue<t, c, p>
you'll see takes 3 template parameters:
- the value type
t
. - the type of underlying container
c
(defaultedstd:vector<t>
). - a binary predicate
p
defining priority order on elements of typet
(defaultedstd::less<...>
).
that is, you'd store whatever need store , define priority using binary predicate. if want store int
objects separate priority could, e.g., store std::pair<int, priority_type>
, provide binary predicate comparing second
element of these pairs.
i realize want implement priority queue entirely follow standard library model (well, move predicate forward , possibly omit customization of underlying container).
Comments
Post a Comment