PHP - Ds Set::allocate() 函式



PHP 的 Ds\Set::allocate() 函式用於為當前集合分配指定容量(或記憶體)以滿足所需容量。如果指定的容量小於或等於當前容量,則它將保持不變。

語法

以下是 PHP Ds\Set::allocate() 函式的語法 -

public void Ds\Set::allocate( int $capacity )

引數

此函式接受一個名為“capacity”的引數,如下所述 -

  • capacity - 要為其分配容量的值的數量。

返回值

此函式不返回任何值。

示例 1

以下程式演示了 PHP Ds\Set::allocate() 函式的使用。

<?php
   $set = new \Ds\Set();
   echo "The capacity of the set (initial): ";
   var_dump($set->capacity());
   $val = 10;
   echo "The capacity need to be allocated: ".$val;
   #using the allocate() function
   $set->allocate($val);
   echo "\nThe capacity of the set after allocating new capacity: ";
   #using capacity() function
   var_dump($set->capacity());
?>

輸出

執行上述程式後,它將顯示以下輸出 -

The capacity of the set (initial): int(8)
The capacity need to be allocated: 10
The capacity of the set after allocating new capacity: int(16)

示例 2

以下是 PHP Ds\Set::allocate() 函式的另一個示例。我們使用此函式為該集合 ([]) 分配新的指定容量 16 和 64。

<?php
   $set = new \Ds\Set();
   echo "The capacity of the set (initial): ";
   var_dump($set->capacity());
   $val1 = 16;
   echo "The first capacity value: ".$val1;
   #using allocate() function
   $set->allocate($val1);
   echo "\nThe capacity of the set after the first capacity allocated: ";
   #using capacity() function
   var_dump($set->capacity());
   $val2 = 64;
   echo "The second capacity value: ".$val2;
   $set->allocate($val2);
   echo "\nThe capacity of the set after the second capacity allocated: ";
   var_dump($set->capacity());
?>

輸出

上述程式顯示以下輸出 -

The capacity of the set (initial): int(8)
The first capacity value: 16
The capacity of the set after the first capacity allocated: int(16)
The second capacity value: 64
The capacity of the set after the second capacity allocated: int(64)
php_function_reference.htm
廣告