日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

集合——PHP实现

發布時間:2025/7/25 php 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 集合——PHP实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

集合:由一個或多個確定的元素所構成的整體叫做集合。是數學中的的常用概念。

集合中的元素有三個特征

1.確定性(集合中的元素必須是確定的)

2.互異性(集合中的元素互不相同。例如:集合A={1,a},則a不能等于1)

3.無序性(集合中的元素沒有先后之分),如集合{3,4,5}和{3,5,4}算作同一個集合。

針對集合這種數據結構的特點,實現如下:

1 <?php 2 class Collection{ 3 protected $_members=array(); 4 protected $_itemHasType; //標志是否指定元素的類型 5 protected $_itemType; //集合中的元素類型 6 7 public function __construct($items = [], $itemHasType = false, $itemType = NULL){ 8 $this->_itemHasType = $itemHasType; 9 $this->_itemType = $itemType; 10 $tmp = $this->getArrayableItems($items); 11 if($this->_itemHasType){ 12 foreach($tmp as $val){ 13 if($val instanceof $this->_itemType){ 14 $this->_members[] = $val; 15 } 16 } 17 }else{ 18 $this->_members = $tmp; 19 } 20 } 21 22 public function addItem($obj){ 23 if($this->_itemHasType){ 24 if(!($obj instanceof $this->_itemType)) 25 throw new Exception("The added obj is not the type of \"$this->_itemType\"!"); 26 } 27 if($this->exists($obj)){ 28 throw new Exception("Obj is already exists!"); 29 }else{ 30 $this->_members[] = $obj; 31 } 32 } 33 34 public function removeItem($obj){ 35 if(false != ($key = array_search($obj, $this->_members))){ 36 unset($this->_members[$key]); 37 }else{ 38 throw new Exception("Obj is not exists!"); 39 } 40 } 41 42 public function all(){ 43 return $this->_members; 44 } 45 46 public function length(){ 47 return sizeof($this->_members); 48 } 49 50 public function exists($obj){ 51 return in_array($obj, $this->_members); 52 } 53 54 protected function getArrayableItems($items) 55 { 56 if (is_array($items)) { 57 return array_values(array_unique($items, SORT_REGULAR)); 58 } elseif ($items instanceof self) { 59 return $items->all(); 60 } 61 return (array) $items; 62 } 63 }

?

該集合類包含如下屬性:

  • protected $_member? 屬性存儲了集合中全部的元素
  • protected $_itemHasType 標志是否需要統一集合中元素的類型(即集合中所有的元素必須是統一類型),默認是false,表示對集合中元素的類型不做限制。若設為true,則限制集合元素必須是$_itemType類型。
  • protected $_itemType? 指定集合中元素的類型。與$_itemHasType配合使用,默認值為NULL,在構造函數中初始化。
  • 該集合類包含了如下方法:

  • public function?__construct($items = [], $itemHasType = false, $itemType = NULL)
    構造方法。其中在通過array_unique方法去除$items數組中的重復項(注意:該方法默認采用字符串方式比較數組元素,需要設置為SORT_REGULAR按照通常的方法比較,這樣才能)
  • protected function getArrayableItems($items)
    對構造方法中輸入的用于初始化的集合元素進行去重去鍵值預處理,或將一個已有的Collection對象中的元素提取出來。需要指出的是:array_unique()方法默認采用字符串方式比較數組元素,需要設置為SORT_REGULAR按照通常的方法比較,這樣才能實現對對象的比較。另外,去鍵值操作是將鍵值統一為1、2、3...的數字,這樣實現array與集合概念的一致,并且避免了在后續查找集合元素時,key值可能影響in_array()方法、array_search()在loose模式下的查找判斷(鍵值對這兩個方法造成的影響可以參考:http://php.net/manual/zh/function.in-array.php#106319),雖然strict模式可以避免key值對in_array()、array_search()方法的影響,但是在strict模式又引發了in_array()、array_search()方法不能對數組中保存的對象的按值比較,只有當是要查找的對象與數組中保存的對象保存的是相同的object identifier時,in_array()才會返回true,array_search()才會返回對應的key值。
  • public function addItem($obj)
    添加元素方法。
  • public function removeItem($obj)
    移除指定元素方法。
  • public function all()
    獲取集合中所有元素,并保存在數組中。
  • public function length()
    獲取集合的長度,即集合中元素的個數。
  • public function exists($obj)
    判斷集合中是否存在指定元素。
  • 集合的應用實例:

    網易筆試題:已知輸入w、x、y、z,有 0<w<=p<=x, 0<y<=q<=z,且其中w、x、y、z、p、q均為正整數。將p/q加入集合中,集合中一共有多少元素。

    1 <?php 2 class Collection{ 3 protected $_members=array(); 4 protected $_itemHasType; //標志是否指定元素的類型 5 protected $_itemType; //集合中的元素類型 6 7 public function __construct($items = [], $itemHasType = false, $itemType = NULL){ 8 $this->_itemHasType = $itemHasType; 9 $this->_itemType = $itemType; 10 $tmp = $this->getArrayableItems($items); 11 if($this->_itemHasType){ 12 foreach($tmp as $val){ 13 if($val instanceof $this->_itemType){ 14 $this->_members[] = $val; 15 } 16 } 17 }else{ 18 $this->_members = $tmp; 19 } 20 } 21 22 public function addItem($obj){ 23 if($this->_itemHasType){ 24 if(!($obj instanceof $this->_itemType)) 25 throw new Exception("The added obj is not the type of \"$this->_itemType\"!"); 26 } 27 if($this->exists($obj)){ 28 //throw new Exception("Obj is already exists!"); 29 }else{ 30 $this->_members[] = $obj; 31 } 32 } 33 34 public function removeItem($obj){ 35 if(false != ($key = array_search($obj, $this->_members))){ 36 unset($this->_members[$key]); 37 }else{ 38 throw new Exception("Obj is not exists!"); 39 } 40 } 41 42 public function all(){ 43 return $this->_members; 44 } 45 46 public function length(){ 47 return sizeof($this->_members); 48 } 49 50 public function exists($obj){ 51 return in_array($obj, $this->_members); 52 } 53 54 protected function getArrayableItems($items) 55 { 56 if (is_array($items)) { 57 return array_values(array_unique($items, SORT_REGULAR)); 58 } elseif ($items instanceof self) { 59 return $items->all(); 60 } 61 return (array) $items; 62 } 63 } 64 65 class Num{ 66 private $numerator; //分子 67 private $denominator; //分母 68 public function __construct($numerator,$denominator){ 69 list($this->numerator, $this->denominator) = $this->simply($numerator, $denominator); 70 } 71 private function simply($numerator,$denominator){ 72 $num = $this->gcd($numerator, $denominator); 73 return array($numerator/$num, $denominator/$num); 74 } 75 76 //獲取兩個數的最大公約數greatest common divisor 77 //輾轉相除法。另外還可以采用更相損減法 78 private function gcd($num1,$num2){ 79 if($num1 < $num2) $this->gcd($num2, $num1); 80 while($num2 != 0){ 81 $tmp = $num1%$num2; 82 $num1 = $num2; 83 $num2 = $tmp; 84 } 85 return $num1; 86 } 87 } 88 89 function getInput(){ 90 return list($w, $x, $y, $z) = explode(' ', trim(fgets(STDIN))); 91 } 92 93 function main(){ 94 list($w, $x,$y,$z) = getInput(); 95 $w = (int)$w; 96 $x = (int)$x; 97 $y = (int)$y; 98 $z = (int)$z; 99 $numSet = new Collection([], true, 'Num'); 100 for($i=$w; $i<=$x; $i++){ 101 for($j=$y; $j<=$z; $j++){ 102 $numSet->addItem(new Num($i,$j)); 103 } 104 } 105 echo $numSet->length(); 106 } 107 main(); View Code

    ?

    轉載于:https://www.cnblogs.com/jade640/p/6634614.html

    總結

    以上是生活随笔為你收集整理的集合——PHP实现的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。