【发布时间】:2015-02-12 08:47:49
【问题描述】:
我想知道如何多次迭代 SplHeap 或 SplPriorityQueue。
next() 方法删除最后一项,因此第二个 foreach(或 for)没有得到任何结果。
例子:
<?php
class Teste extends SplPriorityQueue {}
$teste = new Teste();
$teste->insert( 'A', 1 );
$teste->insert( 'B', 3 );
$teste->insert( 'D', 5 );
$teste->insert( 'C', 2 );
$teste->insert( 'A', 4 );
echo '<pre>';
var_dump( $teste->count() );
echo '<br>';
foreach( $teste as $t )
var_dump( $t );
echo '<br>';
var_dump( $teste->count() );
返回:
int(5)
string(1) "D"
string(1) "A"
string(1) "B"
string(1) "C"
string(1) "A"
int(0) <--- I need this to still be 5
我需要基于 compare() 方法插入项目,如下所示:http://php.net/manual/en/splheap.compare.php
谢谢!
【问题讨论】: