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. 洛谷 P2562 [AHOI2002]Kitty猫基因编码

    P2562 [AHOI2002]Kitty猫基因编码 题目描述 小可可选修了基础生物基因学.教授告诉大家 Super Samuel 星球上 Kitty猫的基因的长度都是 2 的正整数次幂 ), 全是由 ...

  2. iOS Threading编程指南 官方文档翻译第一篇(序言)

    序言   Thread是能够使多个code paths 在同一个APP内并发运行的几种技术之一.虽然新的技术为并发运行提供了先进.高效的工具(例如operation 对象和GCD),但是OS X和iO ...

  3. Myeclipse的默认工作区间怎么恢复提示框?

    好久一直使用默认工作空间.现在,回过头来想让那个提示框回来. 该如何做呢? 1.找到我们的myeclipse安装目录下的 2.false是关闭. 3.改成true 4.同时,新增新的工作区间和之前旧的 ...

  4. 1.1selenium 介绍

    1.1selenium 介绍selenium 是一个 web 的自动化测试工具,不少学习功能自动化的同学开始首选 selenium , 相因为它相比 QTP 有诸多有点:* 免费,也不用再为破解 QT ...

  5. Keepalived + Mysql 双主

    VIP 192.168.1.41 Master 192.168.1.42 Slave 192.168.1.43 .配置 yum -y install mysql-server chkconfig -- ...

  6. VMware Windows安装详细过程(详细图解)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一.概论 简介:虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算 ...

  7. [Angular] AuthService and AngularFire integration

    Config AngularFire, we need database and auth module from firebase. import {NgModule} from '@angular ...

  8. vue2.0 踩坑记录之组件

    - did you register the component correctly? For recursive components, make sure to provide the " ...

  9. MyEclipse中 使用svn更新jar包 出现 svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted 导致整个svn异常处理

    svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted 2014-07-02 ...

  10. Java反射机制的简单应用

    一直感觉java的反射机制非常强大,可是可用的地方不多.在android学习的时候.一直想实现挂断电话的功能,可是系统并没有提供开放的api接口,看了一下网上使用反射机制来实现该功能,确实非常强大,非 ...