>  기사  >  php教程  >  SPL 표준 라이브러리에서 일반적으로 사용되는 데이터 구조

SPL 표준 라이브러리에서 일반적으로 사용되는 데이터 구조

WBOY
WBOY원래의
2016-09-14 09:23:591083검색

스택 데이터 구조

<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>

큐 데이터 구조

<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>

힙 데이터 구조

<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>

고정 길이 배열 데이터 구조

<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>

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:WeChat 기업 계정 SDK다음 기사:WeChat 기업 계정 SDK