php标准库中的优先队列SplPriorityQueue怎么使用?(继承)

一、总结

1、new对象,然后通过insert方法和extract方法来使用,top方法也很常用。

2、类的话首先想到继承,所以可以继承SplPriorityQueue来实现自己特定功能的优先队列。(继承思想)

二、php标准库中的优先队列SplPriorityQueue怎么使用?

而优先队列SplPriorityQueue是基于(后文介绍)实现的。

SplPriorityQueue简单使用:

 $pq = new SplPriorityQueue();

 $pq->insert('a', 10);
$pq->insert('b', 1);
$pq->insert('c', 8); echo $pq->count() .PHP_EOL; //
echo $pq->current() . PHP_EOL; //a /**
* 设置元素出队模式
* SplPriorityQueue::EXTR_DATA 仅提取值
* SplPriorityQueue::EXTR_PRIORITY 仅提取优先级
* SplPriorityQueue::EXTR_BOTH 提取数组包含值和优先级
*/
$pq->setExtractFlags(SplPriorityQueue::EXTR_DATA); while($pq->valid()) {
print_r($pq->current()); //a c b
$pq->next();
}

三、php手册:SplPriorityQueue

简介

The SplPriorityQueue class provides the main functionalities of a prioritized queue, implemented using a max heap.

类摘要

SplPriorityQueue implements Iterator , Countable {
/* 方法 */
public __construct ( void )
public int compare ( mixed $priority1 , mixed $priority2 )
public int count ( void )
public mixed current ( void )
public mixed extract ( void )
public int getExtractFlags ( void )
public void insert ( mixed $value , mixed $priority )
public bool isCorrupted ( void )
public bool isEmpty ( void )
public mixed key ( void )
public void next ( void )
public void recoverFromCorruption ( void )
public void rewind ( void )
public void setExtractFlags ( int $flags )
public mixed top ( void )
public bool valid ( void )

}

Table of Contents

 quick implementation of SPL Priority Queue: 

 <?php 

 class PQtest extends SplPriorityQueue
{
public function compare($priority1, $priority2)
{
if ($priority1 === $priority2) return 0;
return $priority1 < $priority2 ? -1 : 1;
}
} $objPQ = new PQtest(); $objPQ->insert('A',3);
$objPQ->insert('B',6);
$objPQ->insert('C',1);
$objPQ->insert('D',2); echo "COUNT->".$objPQ->count()."<BR>"; //mode of extraction
$objPQ->setExtractFlags(PQtest::EXTR_BOTH); //Go to TOP
$objPQ->top(); while($objPQ->valid()){
print_r($objPQ->current());
echo "<BR>";
$objPQ->next();
} ?> output: COUNT->4
Array ( [data] => B [priority] => 6 )
Array ( [data] => A [priority] => 3 )
Array ( [data] => D [priority] => 2 )
Array ( [data] => C [priority] => 1 )

四、测试题-简答题

1、SplPriorityQueue的入队方法和出队方法是什么?

解答:insert和extract。top方法也很常用。

2、如何遍历一个SplPriorityQueue?

解答:valid()+current()+next()。

29 while($objPQ->valid()){
30 print_r($objPQ->current());
31 echo "<BR>";
32 $objPQ->next();
33 }

3、SplPriorityQueue的实现原理是什么,用的那种数组结构?

解答:用的大根堆。using a max heap。

4、如何插入一个元素到SplPriorityQueue中去?

解答:$objPQ->insert('A',3);  先值后优先级,是一个map。

5、如何写符合自己功能的优先队列(通过继承)?(有类先想继承)

解答:继承SplPriorityQueue类即可,class PQtest extends SplPriorityQueue 。

6、如何设置SplPriorityQueue的提取数据方式,是值是优先级还是两者都提取?

解答:通过SPLPriorityQueue的setExtractFlags方法,$pq->setExtractFlags(SplPriorityQueue::EXTR_DATA);

php标准库中的优先队列SplPriorityQueue怎么使用?(继承)的更多相关文章

  1. STL笔记(6)标准库:标准库中的排序算法

    STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...

  2. 用CAS操作实现Go标准库中的Once

    Go标准库中提供了Sync.Once来实现"只执行一次"的功能.学习了一下源代码,里面用的是经典的双重检查的模式: // Once is an object that will p ...

  3. 彻底弄清c标准库中string.h里的常用函数用法

    在我们平常写的c/c++程序,一些算法题中,我们常常会用到c标准库中string.h文件中的函数,这些函数主要用于处理内存,字符串相关操作,是很有用的工具函数.而且有些时候,在笔试或面试中也会出现让你 ...

  4. 通过atomic_flag简单自旋锁实现简单说明标准库中锁使用的memory_order

    在使用标准库中的加锁机制时,例如我们使用std::mutex,写了如下的代码(下面的代码使用condition_variable可能更合适) std::mutex g_mtx; int g_resNu ...

  5. Python 标准库中的装饰器

    题目描述 1.简单举例 Python 标准库中的装饰器 2.说说你用过的 Python 标准库中的装饰器 1. 首先,我们比较熟悉,也是比较常用的 Python 标准库提供的装饰器有:property ...

  6. (转)python标准库中socket模块详解

    python标准库中socket模块详解 socket模块简介 原文:http://www.lybbn.cn/data/datas.php?yw=71 网络上的两个程序通过一个双向的通信连接实现数据的 ...

  7. c/c++标准库中的文件操作总结

    1 stdio.h是c标准库中的标准输入输出库 2 在c++中调用的方法 直接调用即可,但是最好在函数名前面加上::,以示区分类的内部函数和c标准库函数. 3 c标准输入输出库的使用 3.1 核心结构 ...

  8. C标准库中atoi的一种可能的实现

    为避免与标准库中的atoi产生歧义, 我将自己编写的函数命名为strToInt, 以下是示例代码 #include <stdio.h> int strToInt(const char *s ...

  9. php标准库中QplQueue队列如何使用?

    php标准库中QplQueue队列如何使用? 一.总结 1.new对象,然后通过enqueue方法和dequeue方法使用. 二.php标准库中QplQueue队列如何使用? 队列这种数据结构更简单, ...

随机推荐

  1. 【2017 Multi-University Training Contest - Team 6】Inversion

    [链接]h在这里写链接 [题意] 给出一个序列,求2~n每一个数,下标不是这个数倍数的最大值是什么? [题解] 把a数组从大到小排序. 每个位置i,逆序枚举b数组,找到第一个对应下标不是i的倍数的,直 ...

  2. 【Todo】Zookeeper系列文章

    http://nileader.blog.51cto.com/1381108/1068033

  3. robot framework 使用三:浏览器兼容性自己主动化

    robot framework 測试浏览器兼容性 上图中黄色圈的地方默认什么都不写.是firefox浏览器,写上ie就是ie浏览器了 firefox最新版本号即可.ie须要设置: 1. IE选项设置的 ...

  4. 玩转Bootstrap(基础) -- (6.导航条基础)

    1.导航条样例 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" ...

  5. JavaScript 倒计时器,闹钟功能

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. app 自动化测试 Appium+python可以运行的代码

    Appium

  7. vue使用(二)

    本节目标:           1.数据路径的三种方式          2.{{}}和v-html的区别 1.绑定图片的路径 方法一:直接写路径 <img src="http://p ...

  8. C语言深度解剖读书笔记

    开始本节学习笔记之前,先说几句题外话.其实对于C语言深度解剖这本书来说,看完了有一段时间了,一直没有时间来写这篇博客.正巧还刚刚看完了国嵌唐老师的C语言视频,觉得两者是异曲同工,所以就把两者一起记录下 ...

  9. oracle函数大全-字符串处理函数

    字符函数——返回字符值 这些函数全都接收的是字符族类型的参数(CHR 除外)并且返回字符值.除了特别说明的之外,这些函数大部分返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据库类 ...

  10. 程序员的困境 - R中国用户组-炼数成金

    原文:http://www.oschina.net/news/43389/the-plight-of-programmer 在大型公司中不能腐蚀自己的学习能力和时间能力. 最近我为一个内核程序员的职位 ...