PHP - Ds Set capacity() 函式



PHP 的 Ds\set::capacity() 函式用於檢索任何集合(例如 set、deque、stack 等)的容量,該函式在此集合上呼叫。此函式返回一個整數,表示集合的當前容量。

語法

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

public int Ds\Set::capacity( void )

引數

此函式不接受任何引數。

返回值

此函式返回集合的當前容量。

示例 1

以下是 PHP Ds\Set::capacity() 函式的基本示例。

<?php 
   $set = new \Ds\Set();
   echo "The capacity of the set is: ";
   var_dump($set-> capacity());
?>

輸出

上述程式產生以下輸出:

The capacity of the set is: int(8)

示例 2

以下是 PHP Ds\Set::capacity() 函式的另一個示例。我們使用此函式查詢此集合 ([]) 的容量。

<?php 
   $set = new \Ds\Set([1, 2, 3]);
   echo "The capacity of the set (initial): ";
   print_r($set->capacity());
   $set->allocate(16);
   echo "\nThe capacity of the set after allocating new capacity: ";
   print_r($set->capacity());
?>

輸出

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

The capacity of the set (initial): 8
The capacity of the set after allocating new capacity: 16

示例 3

在下面的示例中,我們使用 capacity() 函式檢索當前集合的容量。

<?php 
   $set = new \Ds\Set([10, 20, 30, 40]);
   echo "The set values are: \n";
   print_r($set);
   echo "The capacity of the set: ";
   print_r(8 * $set->capacity());
?>

輸出

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

The set values are:
Ds\Set Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
)
The capacity of the set: 64
php_function_reference.htm
廣告