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
广告