PHP | Ds\Deque insert()函数
Ds\Deque::insert()函数是PHP中的一个内置函数,用于在 Deque 中的给定索引处插入值。
句法:
public Ds\Deque::insert( $index, $values ) : void
参数:该函数接受上面提到的两个参数,如下所述:
- $index:此参数保存要插入元素的索引。
- $value:此参数保存要插入到 Deque 中给定索引的值。
返回值:此函数不返回任何值。
异常:如果双端队列为空,此函数将抛出OutOfRangeException 。
下面的程序说明了PHP中的Ds\Deque::insert()函数:
方案一:
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
)
方案二:
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。 PHP