STL 中优先队列的使用方法(priority_queu)

基本操作:

empty() 如果队列为空返回真

pop() 删除对顶元素

push() 加入一个元素

size() 返回优先队列中拥有的元素个数

top() 返回优先队列对顶元素

在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。

使用方法:

头文件:

#include <queue>

声明方式:

1、普通方法:

priority_queue<int>q;

//通过操作,按照元素从大到小的顺序出队

2、自定义优先级:

struct cmp

{

operatorbool ()(int x, int y)

{

return x > y; // x小的优先级高

//也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高

}

};

priority_queue<int, vector<int>,
cmp>q;//定义方法

//其中,第二个参数为容器类型。第三个参数为比较函数。

3、结构体声明方式:

struct node

{

int x, y;

friend booloperator< (node a, node b)

{

return a.x > b.x; //结构体中,x小的优先级高

}

};

priority_queue<node>q;//定义方法

//在该结构中,y为值, x为优先级。

//通过自定义operator<操作符来比较元素中的优先级。

//在重载”<”时,最好不要重载”>”,可能会发生编译错误

STL 中队列的使用(queue)

基本操作:

push(x) 将x压入队列的末端

pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值

front() 返回第一个元素(队顶元素)

back() 返回最后被压入的元素(队尾元素)

empty() 当队列为空时,返回true

size() 返回队列的长度

使用方法:

头文件:

#include <queue>

声明方法:

1、普通声明

queue<int>q;

2、结构体

struct node

{

int x, y;

};

queue<node>q;

STL 中栈的使用方法(stack)

基本操作:

push(x) 将x加入栈中,即入栈操作

pop() 出栈操作(删除栈顶),只是出栈,没有返回值

top() 返回第一个元素(栈顶元素)

size() 返回栈中的元素个数

empty() 当栈为空时,返回 true

使用方法:

和队列差不多,其中头文件为:

#include <stack>

定义方法为:

stack<int>s1;//入栈元素为
int 型

stack<string>s2;// 入队元素为string型

stack<node>s3;//入队元素为自定义型


/**//*

*===================================*

| |

| STL中优先队列使用方法 |

| | 

| chenlie |

| |

| 2010-3-24 |

| |

*===================================*

*/

#include <iostream>

#include <vector>

#include <queue>

usingnamespace std;

];



struct cmp1

{

booloperator ()(int x, int y)

{

return x > y;//小的优先级高

}

};



struct cmp2

{

booloperator ()(constint x, constint y)

{

return c[x] > c[y]; 

// c[x]小的优先级高,由于可以在对外改变队内的值,

//所以使用此方法达不到真正的优先。建议用结构体类型。

}

};



struct node

{

int x, y;

friend booloperator< (node a, node b)

{

return a.x > b.x;//结构体中,x小的优先级高

}

};





priority_queue<int>q1;



priority_queue<int, vector<int>,
cmp1>q2;



priority_queue<int, vector<int>,
cmp2>q3;



priority_queue<node>q4;





queue<int>qq1;

queue<node>qq2;



int main()

{

int i, j, k, m, n;

int x, y;

node a;

while (cin >> n)

{

; i < n;
i++)

{

cin >> a.y >> a.x;

q4.push(a);

}

cout << endl;

while (!q4.empty())

{

cout << q4.top().y <<""<< q4.top().x << endl;

q4.pop();

}

// cout << endl;

}

;

}

STL之优先队列的更多相关文章

  1. 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动

    一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...

  2. STL中优先队列的使用

    普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出的行为特征.我们来说一下C++的 ...

  3. STL priority_queue 优先队列 小记

    今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...

  4. STL之优先队列(1)

    优先队列用法 在优先队列中,优先级高的元素先出队列. 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系. 优先队列的第一种用法: 也是最常用的用法 priority_queue< ...

  5. STL之优先队列(priority_queue)

    转自网上大牛博客,原文地址:http://www.cnblogs.com/summerRQ/articles/2470130.html 先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对 ...

  6. W - stl 的 优先队列 Ⅲ

    Description In a speech contest, when a contestant finishes his speech, the judges will then grade h ...

  7. V - stl 的 优先队列 Ⅱ

    Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every morni ...

  8. hdu 4393 Throw nails(STL之优先队列)

    Problem Description The annual school bicycle contest started. ZL is a student in this school. He is ...

  9. hdu1716排列2(stl:next_permutation+优先队列)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

随机推荐

  1. weka 集成学习

    import java.io.*;import weka.classifiers.*;import weka.classifiers.meta.Vote;import weka.core.Instan ...

  2. HW4.34

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  3. C 数据结构1——线性表分析(顺序存储、链式存储)

    之前是由于学校工作室招新,跟着大伙工作室招新训练营学习数据结构,那个时候,纯碎是小白(至少比现在白很多)那个时候,学习数据结构,真的是一脸茫然,虽然写出来了,但真的不知道在干嘛.调试过程中,各种bug ...

  4. ios 免书籍入门站点

    http://www.raywenderlich.com/tutorials http://www.raywenderlich.com/ios-tutorials http://web.stanfor ...

  5. NULL指针 Void* 和野指针

    在C和C++的语言中常常有这几个概念: NULL指针.空指针.Void *指针.野指针(Wild Pointer)甚至垂悬指针(Dangling Pointer). 1.NULL指针,一般用于指向一个 ...

  6. HDU4349--Xiao Ming's Hope(数论)

    输入一个n(1<=n<=108),求C(n,0),C(n,1),C(n,2)...C(n,n)有多少个奇数. Lacus定理 http://blog.csdn.net/acm_cxlove ...

  7. c++学生成绩管理系统

    虽然比较水 =.= 但是写了两节课+一个中午 都是强迫症的锅 http://www.cnblogs.com/wenruo/p/4940182.html #include <cstdio> ...

  8. hadoop-2.6.0.tar.gz + spark-1.5.2-bin-hadoop2.6.tgz 的集群搭建(3节点和5节点皆适用)

    本人呕心沥血所写,经过好一段时间反复锤炼和整理修改.感谢所参考的博友们!同时,欢迎前来查阅赏脸的博友们收藏和转载,附上本人的链接.http://www.cnblogs.com/zlslch/p/584 ...

  9. Redis学习资料汇总(荐)

    Redis学习手册系列: http://www.cnblogs.com/stephen-liu74/category/354125.html Redis详解:sorted sets数据类型及操作-IT ...

  10. advanced dom scripting dynamic web design techniques Part One DOM SCRIPTING IN DETAIL CHAPTER 1 DO IT RIGHT WITH BEST PRACTICES

    You’re excited; your client is excited. All is well. You’ve just launched the client’s latest websit ...