PHP标准库:栈、堆、队列、定长数组
栈:先进后出
$stack = new SplStack();
$stack->push("data1\n");
$stack->push("data2\n");
echo $stack->pop(); # data2
echo "<br>";
echo $stack->pop(); # data1
堆 (不了解)
$minheap = new SplMinHeap();
$minheap->insert("data1\n");
$minheap->insert("data2\n");
echo $minheap->extract(); # data1
echo "<br>";
echo $minheap->extract(); # data2
队列 先进先出
$queue = new SplQueue(); //队列 先进先出
$queue->enqueue("data1\n"); //入队
$queue->enqueue("data2\n");
echo $queue->dequeue(); #data1
echo "<br>";
echo $queue->dequeue(); #data2
定长数组(对象)
$array = new SplFixedArray(10); # 定义 定长数组(对象)
$array[4] = 5; # 定长数组(对象)无论是否使用,都会分配内存空间。
# $array[10] = 10; # 超出分配的内存空间导致500错误。
var_dump($array[4]); # 打印使用过的内存空间
var_dump($array); # 打印全部分配的内存空间
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!