spl是用於解決典型問題(standard problems)的一組介面與類的集合。
此擴充套件只能在php 5.0以後使用,從php 5.3.0 不再被關閉,會一直有效.成為php核心元件一部份。
spl提供了一組標準資料結構。
雙鏈表是一種重要的線性儲存結構,對於雙鏈表中的每個節點,不僅僅儲存自己的資訊,還要儲存前驅和後繼節點的位址。
spldoublylinkedlist
spldoublylinkedlist implements iterator , arrayaccess , countable
接下來是使用方法:
堆$list = new spldoublylinkedlist();
$list->push('a');
$list = new spldoublylinkedlist();
$list->push('a');
$list->push('b');
$list->push('c');
$list->push('d');
$list->unshift('top');
$list->shift();
$list->rewind();//rewind操作用於把節點指標指向bottom所在的節點
echo 'curren node:'.$list->current()."
";//獲取當前節點
$list->next();//指標指向下乙個節點
echo 'next node:'.$list->current()."";
$list->next();
$list->next();
$list->prev();//指標指向上乙個節點
echo 'next node:'.$list->current()."";
if($list->current())
echo 'current node is valid
';else
echo 'current node is invalid
';
if($list->valid())//如果當前節點是有效節點,valid返回true
echo "valid list
";else
echo "invalid list ";
var_dump(array(
'pop' => $list->pop(),
'count' => $list->count(),
'isempty' => $list->isempty(),
'bottom' => $list->bottom(),
'top' => $list->top()
));
$list->setiteratormode(spldoublylinkedlist::it_mode_fifo);
var_dump($list->getiteratormode());
for($list->rewind(); $list->valid(); $list->next())
var_dump($a = $list->serialize());
//print_r($list->unserialize($a));
$list->offsetset(0,'new one');
$list->offsetunset(0);
var_dump(array(
'offsetexists' => $list->offsetexists(4),
'offsetget' => $list->offsetget(0),
));var_dump($list);
//堆疊,先進後出
$stack = new splstack();//繼承自spldoublylinkedlist類
$stack->push("a
");$stack->push("b
");
echo $stack->pop();
echo $stack->pop();
echo $stack->offsetset(0,'b');//堆疊的offset=0是top所在的位置,offset=1是top位置節點靠近bottom位置的相鄰節點,以此類推
$stack->rewind();//雙向鍊錶的rewind和堆疊的rewind相反,堆疊的rewind使得當前指標指向top所在的位置,而雙向鍊錶呼叫之後指向bottom所在位置
echo 'current:'.$stack->current().'
';$stack->next();//堆疊的next操作使指標指向靠近bottom位置的下乙個節點,而雙向鍊錶是靠近top的下乙個節點
echo 'current:'.$stack->current().'
';echo '';
//佇列,先進先出
$queue = new splqueue();//繼承自spldoublylinkedlist類
$queue->enqueue("a
");//插入乙個節點到佇列裡面的top位置
$queue->enqueue("b
");$queue->offsetset(0,'a');//堆疊的offset=0是top所在的位置,offset=1是top位置節點靠近bottom位置的相鄰節點,以此類推
echo $queue->dequeue();
echo $queue->dequeue();
echo "
";
堆(heap)就是為了實現優先佇列而設計的一種資料結構,它是通過構造二叉堆(二叉樹的一種)實現。根節點最大的堆叫做最大堆或大根堆(splmaxheap),根節點最小的堆叫做最小堆或小根堆(splminheap)。二叉堆還常用於排序(堆排序)
splpriorityqueue
abstract splheap implements iterator , countable
使用方法:
//堆
class mysplheap extends splheap}
$obj = new mysplheap();
$obj->insert(0);
$obj->insert(1);
$obj->insert(2);
$obj->insert(3);
$obj->insert(4);
echo $obj->top();//4
echo $obj->count();//5
foreach ($obj as $item)
陣列
優先佇列也是非常實用的一種資料結構,可以通過加權對值進行排序,由於排序在php內部實現,業務**中將精簡不少而且更高效。通過splpriorityqueue::setextractflags(int $flag)設定提取方式可以提取資料(等同最大堆)、優先順序、和兩者都提取的方式。
splfixedarray implements iterator , arrayaccess , countable
使用方法:
$arr = new splfixedarray(4);
$arr[0] = 'php';
$arr[1] = 1;
$arr[3] = 'python';//遍歷, $arr[2] 為null
foreach($arr as $v)
//獲取陣列長度
echo $arr->getsize(); //4
//增加陣列長度
$arr->setsize(5);
$arr[4] = 'new one';
//捕獲異常
try catch (runtimeexception $e)
對映
用來儲存一組物件的,特別是當你需要唯一標識物件的時候。
splobjectstorage implements countable , iterator , serializable , arrayaccess
使用方法:
class a }
$a1 = new a(1);
$a2 = new a(2);
$a3 = new a(3);
$a4 = new a(4);
$container = new splobjectstorage();
//splobjectstorage::attach 新增物件到storage中
$container->attach($a1);
$container->attach($a2);
$container->attach($a3);
//splobjectstorage::detach 將物件從storage中移除
$container->detach($a2);
//splobjectstorage::contains用於檢查物件是否存在storage中
var_dump($container->contains($a1)); //true
var_dump($container->contains($a4)); //false
//遍歷
$container->rewind();
while($container->valid())
深入淺出sizeof
int佔 位元組,short佔 位元組 1.0 回答下列問題 答案在文章末尾 1.sizeof char 2.sizeof a 3.sizeof a 4.strlen a 如果你答對了全部四道題,那麼你可以不用細看下面關於sizeof的論述。如果你答錯了部分題目,那麼就跟著我來一起 關於sizeof...
深入淺出ShellExecute
ipconfig c log.txt應如何處理?二樓的朋友,開啟拔號網路這樣 shellexecute null,open c windows rundll32.exe shell32.dll,control rundll c windows system telephon.cpl null,sw ...
深入淺出ShellExecute
深入淺出shellexecute譯者 徐景周 原作 nishant s q 如何開啟乙個應用程式?shellexecute this m hwnd,open calc.exe sw show 或shellexecute this m hwnd,open notepad.exe c mylog.log...