PHP - Ds Deque::copy() 函式



PHP 的 Ds\Deque::copy() 函式用於檢索當前雙端佇列的淺複製。集合(或雙端佇列)的淺複製是指屬性與從中建立複製的源物件的屬性共享相同引用的複製。

您可以在任何集合(如集合、棧、雙端佇列等)上呼叫此函式。

語法

以下是 PHP Ds\Deque::copy() 函式的語法:

public Ds\Deque::copy(): Ds\Deque

引數

此函式不接受任何引數。

返回值

此函式返回當前雙端佇列的淺複製。

示例 1

以下是 PHP Ds\Deque::copy() 函式的基本示例:

<?php
   $deque = new \Ds\Deque([10, 20, 30, 40, 50]);
   echo "The original deque: \n";
   print_r($deque);
   echo "The shallow copy of a deque: \n";
   print_r($deque->copy());
?>

輸出

以上程式顯示以下輸出:

The original deque:
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The shallow copy of a deque:
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

示例 2

以下是 PHP Ds\Deque::copy() 函式的另一個示例。我們使用此函式來檢索此雙端佇列(["Tutorials", "Point", "India"])的淺複製:

<?php
   $deque = new \Ds\Deque(["Tutorials", "Point", "India"]);
   echo "The original deque: \n";
   print_r($deque);
   echo "The shallow copy of a deque: \n";
   print_r($deque->copy());
?>

輸出

執行以上程式後,將生成以下輸出:

The original deque:
Ds\Deque Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The shallow copy of a deque:
Ds\Deque Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
php_function_reference.htm
廣告