priority_queue操作

优先队列和堆的操作基本相同。优先队列只不过比堆多了一个接口,就是获得优先级最高的值。位于堆顶的元素一般是优先级别最高的,而且插入数据需要重新排列的复杂度为O(lgn)。

#include 
#include 
#include 
using namespace std;
#include 
#include 
struct cmp
{
    int operator ()(char a,char b)
   {
      return a>b;
   }
};
struct node
{
    //char a[10];
    string a;
    string c;
    int b;
    friend bool operator < (node x,node y)
    //注意到:结构体中重载的运算符只可以是 <, 因为标准库默认使用元素类型的 < 操作符来确定它们之间的优先级关系,如果重载 > ,那么会编译错误。
    {
        //return x.by.b;
               return x.c>y.c;
           }
         return x.a>y.a;
    }
};
int main()
{
    //priority_queue p;
    /*priority_queue,cmp> p;
    p.push('s');                  //push
    p.push('w');
    p.push('a');
    p.push('e');
    p.push('d');
    if(p.empty())             //empty
        printf("空\n");
    else
        printf("不空\n");
    for(;p.size();)            //size
    {
        cout< p;
    p.push(n1);
    p.push(n2);
    p.push(n3);
    p.push(n4);
    p.push(n5);
    cout<

C++ Priority Queues(优先队列)

C++优先队列类似队列,但是在这个数据结构中的元素按照一定的断言排列有序。

empty() 如果优先队列为空,则返回真
pop() 删除第一个元素
push() 加入一个元素
size() 返回优先队列中拥有的元素的个数
top() 返回优先队列中有最高优先级的元素

你可能感兴趣的:(STL)