【C++ STL 容器】——优先队列
概念
优先队列即priority_queue类,带优先权的队列,优先权高度元素优先出队。与普通队列相比,共同点都是对队头做删除操作,队尾做插入操作,但不一定遵循先进先出原则,priority_queue是一个基于某个几本序列进行构建的适配器,默认的序列容器是vector。
常用函数
构造函数
- priority_queue(const Pred& pr = Pred(),const allocator_type& al = allocator_type());创建元素类型为T的空优先队列,Pred是二元比较函数,默认是less
。 - priority_queue(const value_type *first,const value_type *last, const Pred& pr = Pred(),const allocator_type& al = allocator_type());以迭代器[first,last)指向元素,创建元素类型为T的优先队列,Pred是二元比较函数,默认是less
.
- priority_queue(const Pred& pr = Pred(),const allocator_type& al = allocator_type());创建元素类型为T的空优先队列,Pred是二元比较函数,默认是less
操作函数
- bool empty();如果优先队列为空返回true,否则返回false。
- int size();返回优先队列中元素数量。
- void push(const T& t);把t元素压入优先队列。
- void pop();优先队列非空情况下,删除优先级最高元素。
- T& top();优先队列非空情况下,返回优先级最高元素的引用。
还没有评论,来说两句吧...