文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > 计算机软件及应用 > C++ Priority Queues(优先队列)
侵权投诉

C++ Priority Queues(优先队列)

C++ Priority Queue(优先队列)
C++优先队列类似队列,但是在这个数据结构中的元素按照一定的断言排列有序。它的头文件为<queue>。由于适配器不支持迭代,一个 priority_queue 将有没有关联的迭代器。

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

/////////////////////////////////////////////////////////////////////////////////////

构造函数
explicit priority_queue(const Pred& pr = Pred(),
const allocator_type& al = allocator_type());
priority_queue(const value_type *first, const value_type *last,
const Pred& pr = Pred(), const allocator_type& al = allocator_type());

empty
语法:
bool empty();
empty()函数返回真(true)如果优先队列为空,否则返回假(false)。
pop
语法:
void pop();
pop()函数删除优先队列中的第一个元素。
push
语法:
void push( const TYPE &val );
push()函数添加一个元素到优先队列中,值为val。
size
语法:
size_type size();
size()函数返回优先队列中存储的元素个数。
top
语法:
TYPE &top();
top()返回一个引用,指向优先队列中有最高优先级的元素。注意只有pop()函数删除一个元素。
示例1:
#include <iostream>
#include <queue>
#include <deque>
#include <vector>
#include <functional>
using namespace std;

#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif

// Using priority_queue with deque
// Use of function greater sorts the items in ascending order
typedef deque<int, allocator<int> > INTDQU;
typedef priority_queue<int,INTDQU, greater<int> > INTPRQUE;

// Using priority_queue with vector
// Use of function less sorts the items in descending order
typedef vector<char, allocator<char> > CHVECTOR;
typedef priority_queue<char,CHVECTOR,less<char> > CHPRQUE;

void main(void)
{

int size_q;
INTPRQUE q;
CHPRQUE p;

// Insert items in the priority_queue(uses deque)
q.push(42);
q.push(100);
q.push(49);
q.push(201);

// Output the item at the top using top()
cout << q.top() << endl;
// Output the size of priority_queue
size_q = q.size();
cout << "size of q is:" << size_q << endl;
// Output items in priority_queue using top()
// and use pop() to get to next item until
// priority_queue is empty
while (!q.empty())
{
cout << q.top() << endl;
q.pop();
}

// Insert items in the priority_queue(uses vector)
p.push('c');
p.push('a');
p.push('d');
p.push('m');
p.push('h');

// Output the item at the top using top()
cout << p.top() << endl;

// Output the size of priority_queue

第1页

你可能喜欢

热门文档

相关文档

  • C++优先队列的基本使用方法

    {3,5,9,6,2}; //示例 1 priority_queue<int> qi;//普通的优先级队列,按从大到小排序 for(i = 0; i < len; i++) qi.push(a[i]); for(i ...

  • C++ Queues(队列)

    C++ Queue(队列) queue是容器配接器C的一个示例,容器配接器C将一些基础容器...数据结构Queues 队列 49页 免费 Priority Queues 34页 免费 priority queues 37...

  • 编写优先队列数据(priority_queue)

    C++ Priority Queues(优先... 2页 免费 priority_queue 7页 8财富值 Priority...if (y >= heap[c]) break; // 能 // 不能 heap[c/2] = heap[c]...

  • priority_queue

    现在在这里说说用法吧 先看看语法: Syntax: In their implementation in the C++ Standard Template Library, priority queues take three template parameters:1 2 ...

  • C Double-Ended Queues(双向队列)

    C Double-Ended Queues(双向队列)_计算机软件及应用_IT/计算机_专业资料。 C++...数据结构-队列-Queues 12页 1下载券 C++ Priority Queues(优... 2页 免费 ...

  • C++ 标准模板库(STL)

    () 在末尾加入一个元素 6.size() 返回队列中元素的个数 4.4 C++ Priority Queues(优先队列) C++优先队列类似队列,但是在这个数据结构中的元素按照一定的断言...

  • chap07_01 Priority(Heaps)

    数据结构(Data Structures) (C语言版) 主讲教师: 吴让仲 Instructor: WU, RANGZHONG...(武汉)机电学院通信系 2012年秋季 1/8 CHAPTER 7 PRIORITY QUEUES优先队列 ...

  • 数据结构、算法与应用(Sartej Sahni)-9

    Priority Queues ? CHAPTER 9 ? Priority Queues (优先队列) ? ? 1) Find ...c = 2*i; // c的父节点是y的目标位置 while (c <= CurrentSize) { /...

  • C++ 标准模板库(STL)学习总结

    pop()函数删除队列的一个元素。 push()函数往队列中加入一个元素。 size()返回队列中元素的个数。 3.C++ Priority Queues(优先队列) empty() 函数返回真(...

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email