略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Ds\Deque::unshift

2024-12-21

Ds\Deque::unshift

(PECL ds >= 1.0.0)

Ds\Deque::unshiftAdds values to the front of the deque

说明

public Ds\Deque::unshift(mixed $values = ?): void

Adds values to the front of the deque, moving all the current values forward to make room for the new values.

参数

values

The values to add to the front of the deque.

注意:

Multiple values will be added in the same order that they are passed.

返回值

没有返回值。

范例

示例 #1 Ds\Deque::unshift() example

<?php
$deque 
= new \Ds\Deque([123]);

$deque->unshift("a");
$deque->unshift("b""c");

print_r($deque);
?>

以上例程的输出类似于:

Ds\Deque Object
(
    [0] => b
    [1] => c
    [2] => a
    [3] => 1
    [4] => 2
    [5] => 3
)
add a noteadd a note

User Contributed Notes 1 note

up
1
hugo at fc dot up dot pt
1 year ago
Where the documentation says "moving all the current values forward", while it sounds like the complexity would be O(N), I checked the code at https://github.com/php-ds/ext-ds/blob/master/src/ds/ds_deque.c#L366 and it is in fact O(1) for each element

官方地址:https://www.php.net/manual/en/ds-deque.unshift.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3