PHP如何使用Ds Mapalloc()函数?代码示例

2021年3月27日18:17:24 发表评论 1,173 次浏览

Ds\Map :: allocate()function是PHP中的内置函数, 用于为所需容量分配足够的内存。

语法如下:

void public Ds\Map::allocate( $capacity )

参数:该函数接受单个参数$容量指示已分配容量的数量。

返回值:此函数不返回任何值。

下面的程序说明了Ds \ Map :: allocate()PHP中的功能:

程序1:

<?php 
  
// Create new map 
$map = new \Ds\Map(); 
  
// Use capacity() function to 
// display the capacity of map
var_dump( $map ->capacity()); 
  
// Allocate capacity 
$map ->allocate(50); 
  
// Display capacity 
var_dump( $map ->capacity()); 
  
// Allocate capacity 
$map ->allocate(80); 
  
// Display capacity 
var_dump( $map ->capacity()); 
?>

输出如下:

int(8)
int(64)
int(128)

程式2:

<?php 
  
// Create new map 
$map = new \Ds\Map(); 
  
// Declare capacity array 
$arr = array (10, 20, 30, 40); 
  
// Loop run for every array element 
foreach ( $arr as $val ) { 
      
     // Allocate capacity 
     $map ->allocate( $val ); 
  
     // Display capacity of map
     var_dump( $map ->capacity()); 
} 
  
?>

输出如下:

int(16)
int(32)
int(32)
int(64)

参考: https://www.php.net/manual/en/ds-map.allocate.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: