PHP Ds PriorityQueue peek()函数用法介绍

2021年3月16日16:41:12 发表评论 687 次浏览

Ds \ PriorityQueue :: peek()PHP中的函数用于获取出现在PriorityQueue前面的值。

语法如下:

mixed public Ds\PriorityQueue::peek ( void )

参数:该函数不接受任何参数。

返回值:此函数返回此PriorityQueue前面的值。函数的返回类型是混合的, 并且取决于存储在PriorityQueue中的值的类型。

例外:如果PriorityQueue为空, 则此函数将引发UnderflowException。

下面的程序说明了Ds \ PriorityQueue :: peek():

程序1:

<?php 
  
// Declare new PriorityQueue 
$pq = new \Ds\PriorityQueue(); 
  
// Add elements to the PriorityQueue
$pq ->push( "One" , 1);
$pq ->push( "Two" , 2);
$pq ->push( "Three" , 3);
  
echo "PriorityQueue is: \n" ;
print_r( $pq );
  
// Get element at the front
echo "\nElement at front is: " ;
print_r( $pq ->peek());
  
?>

输出如下:

PriorityQueue is: 
Ds\PriorityQueue Object
(
    [0] => Three
    [1] => Two
    [2] => One
)

Element at front is: Three

程式2:

<?php 
  
// Declare new PriorityQueue 
$pq = new \Ds\PriorityQueue(); 
  
echo "PriorityQueue is: \n" ;
print_r( $pq );
  
// Get element at the front
echo "\nElement at front is: " ;
print_r( $pq ->peek());
  
?>

输出如下:

PHP Fatal error:  Uncaught UnderflowException

参考: http://php.net/manual/en/ds-priorityqueue.peek.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: