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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
