函数名:SplHeap::current()
适用版本:所有PHP版本
函数描述:SplHeap::current()方法用于获取当前指针位置的元素值。
用法:
public function current (): mixed
示例:
// 创建一个继承SplHeap的自定义堆类
class MyHeap extends SplHeap {
// 实现抽象方法,定义元素比较规则
protected function compare($value1, $value2) {
return $value1 - $value2;
}
}
// 创建堆对象
$heap = new MyHeap();
// 向堆中添加元素
$heap->insert(5);
$heap->insert(3);
$heap->insert(8);
// 循环遍历堆中的元素
foreach ($heap as $value) {
echo $heap->current() . PHP_EOL; // 获取当前元素值
$heap->next(); // 指针移动到下一个元素
}
输出结果:
8
5
3
注意事项: