PHP - Ds Sequence::capacity() 函式



PHP 的 Ds\Sequence::capacity() 函式用於檢索序列的當前容量。“容量”指的是序列可以容納的記憶體或元素數量。

PHP Ds\Sequence 類提供另一個名為 allocate() 的函式,用於為當前序列分配足夠的容量。

語法

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

abstract public Ds\Sequence::capacity(): int

引數

此函式不接受任何引數。

返回值

此函式返回序列的當前容量。

示例 1

以下程式演示了 PHP Ds\Sequence::capacity() 函式的使用:

<?php 
   $seq = new \Ds\Vector([1, 2, 3]);
   echo "The sequence elements are: \n";
   print_r($seq);
   echo "The current capacity of a sequence is: ";
   #using capacity() function
   print_r($seq->capacity());
?>

輸出

上述程式生成以下輸出:

The sequence elements are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
The current capacity of a sequence is: 8

示例 2

以下是 PHP Ds\Sequence::capacity() 函式的另一個示例。我們使用此函式來檢索此序列(["Tutorials", "Point", "India"]) 的當前容量:

<?php 
   $seq = new \Ds\Stack(["Tutorials", "Point", "India"]);
   echo "The sequence elements are: \n";
   print_r($seq);
   echo "The capacity before allocating: ";
   #using capacity() function
   print($seq->capacity());
   #using allocate() function
   $seq->allocate(32);
   echo "\nThe capacity after allocating: ";
   print($seq->capacity());
?>

輸出

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

The sequence elements are:
Ds\Stack Object
(
    [0] => India
    [1] => Point
    [2] => Tutorials
)
The capacity before allocating: 8
The capacity after allocating: 32
php_function_reference.htm
廣告