Ds \ Deque::insert()函數是PHP中的內置函數,用於在Deque中的給定索引處插入值。
用法:
public Ds\Deque::insert( $index, $values ):void
參數:該函數接受上麵提到並在下麵描述的兩個參數:
- $index:此參數保存要插入元素的索引。
- $value:此參數保存要插入Deque中給定索引的值。
返回值:該函數不返回任何值。
異常:如果雙端隊列為空,則此函數將引發OutOfRangeException。
以下示例程序旨在說明PHP中的Ds \ Deque::insert()函數:
程序1:
<?php
// Declare a deque
$deck = new \Ds\Deque([1, 2, 3, 4, 5, 6]);
echo("Elements in the Deque\n");
// Display the Deque elements
print_r($deck);
echo("\nInsert 10 at index 4 in the deque\n");
// Use insert() function to inserting
// element in the deque at index 4
$deck->insert(4, 10);
// Display the Deque elements
print_r($deck);
?>
輸出:
Elements in the Deque Ds\Deque Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) Insert 10 at index 4 in the deque Ds\Deque Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 10 [5] => 5 [6] => 6 )
程序2:
<?php
// Declare a deque
$deck = new \Ds\Deque([1, 2, 3, 4, 5, 6]);
echo("Original Deque\n");
// Display the Deque elements
print_r($deck);
echo("\nModified Deque\n");
// Use insert() function to inserting
// element in the deque at index 4
$deck->insert(3, ...[10, 20, 30, 40]);
// Display the Deque elements
print_r($deck);
?>
輸出:
Original Deque Ds\Deque Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) Modified Deque Ds\Deque Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 10 [4] => 20 [5] => 30 [6] => 40 [7] => 4 [8] => 5 [9] => 6 )
參考: http://php.net/manual/en/ds-deque.insert.php
相關用法
注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 PHP | Ds\Deque insert() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。