Heim  >  Artikel  >  php教程  >  Häufig verwendete Datenstrukturen in der SPL-Standardbibliothek

Häufig verwendete Datenstrukturen in der SPL-Standardbibliothek

WBOY
WBOYOriginal
2016-09-14 09:23:591083Durchsuche

Stack-Datenstruktur

<span style="color: #008080;">1</span> <span style="color: #800080;">$stack</span> = <span style="color: #0000ff;">new</span> SplStack();    <span style="color: #008000;">//</span><span style="color: #008000;">栈数据结构->先进后出    </span>
<span style="color: #008080;">2 </span><span style="color: #800080;">$stack</span>->push('data1');      <span style="color: #008000;">//</span><span style="color: #008000;">入栈</span>
<span style="color: #008080;">3</span> <span style="color: #800080;">$stack</span>->push('data2');      <span style="color: #008000;">//</span><span style="color: #008000;">入栈</span>
<span style="color: #008080;">4</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$stack</span>->pop();         <span style="color: #008000;">//</span><span style="color: #008000;">出栈->data2</span>
<span style="color: #008080;">5</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$stack</span>->pop();         <span style="color: #008000;">//</span><span style="color: #008000;">出栈->data1</span>

Warteschlangendatenstruktur

<span style="color: #008080;">1</span> <span style="color: #800080;">$queue</span> = <span style="color: #0000ff;">new</span> SplQueue();    <span style="color: #008000;">//</span><span style="color: #008000;">队列数据结构->先进先出</span>
<span style="color: #008080;">2</span> <span style="color: #800080;">$queue</span>->enqueue('data1');    <span style="color: #008000;">//</span><span style="color: #008000;">入列</span>
<span style="color: #008080;">3</span> <span style="color: #800080;">$queue</span>->enqueue('data2');    <span style="color: #008000;">//</span><span style="color: #008000;">入列</span>
<span style="color: #008080;">4</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$queue</span>->dequeue();    <span style="color: #008000;">//</span><span style="color: #008000;">出列->data1</span>
<span style="color: #008080;">5</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$queue</span>->dequeue();    <span style="color: #008000;">//</span><span style="color: #008000;">出列->data2</span>

Heap-Datenstruktur

<span style="color: #008080;">1</span> <span style="color: #800080;">$heap</span> = <span style="color: #0000ff;">new</span> SplMinHeap();    <span style="color: #008000;">//</span><span style="color: #008000;">堆数据结构</span>
<span style="color: #008080;">2</span> <span style="color: #800080;">$heap</span>->insert('data1');        <span style="color: #008000;">//</span><span style="color: #008000;">存放</span>
<span style="color: #008080;">3</span> <span style="color: #800080;">$heap</span>->insert('data2');        <span style="color: #008000;">//</span><span style="color: #008000;">存放</span>
<span style="color: #008080;">4</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$heap</span>-><span style="color: #008080;">extract</span>();        <span style="color: #008000;">//</span><span style="color: #008000;">取出->data1</span>
<span style="color: #008080;">5</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$heap</span>-><span style="color: #008080;">extract</span>();        <span style="color: #008000;">//</span><span style="color: #008000;">取出->data2</span>

Array-Datenstruktur fester Länge

<span style="color: #008080;">1</span> <span style="color: #800080;">$array</span> = <span style="color: #0000ff;">new</span> SplFixedArray(10);    <span style="color: #008000;">//</span><span style="color: #008000;">固定长度的数组数据结构,声明长度为10</span>
<span style="color: #008080;">2</span> <span style="color: #800080;">$array</span>[0] = 0<span style="color: #000000;">;
</span><span style="color: #008080;">3</span> <span style="color: #800080;">$array</span>[9] = 9<span style="color: #000000;">;
</span><span style="color: #008080;">4</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$array</span><span style="color: #000000;">);    
</span><span style="color: #008080;">5</span> <span style="color: #008000;">//</span><span style="color: #008000;">object(SplFixedArray)#1 (10) { [0]=> int(0) [1]=> NULL [2]=> NULL [3]=> NULL [4]=> NULL [5]=> NULL [6]=> NULL [7]=> NULL [8]=> NULL [9]=> int(9) }
</span><span style="color: #008080;">6</span> <span style="color: #008000;">//下标1-8无论有没有使用,都会分配内存空间</span>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:WeChat Enterprise Account SDKNächster Artikel:WeChat Enterprise Account SDK