Recently I have read about hash-tables in a very famous book "Introduction to Algorithms". I haven't used them in any real applications yet, but want to. But I don't know how to start.
Can anyone give me some samples of using it, for example, how to realize a dictionary application (like ABBYY Lingvo) using hash-tables?
And finally I would like to know what is the difference between hash-tables and associative arrays in PHP, I mean which technology should I use and in which situations?
If I am wrong (I beg pardon) please correct me, because actually I am starting with hash-tables and I have just basic (theoretical) knowledge about them.
Thanks a lot.
散列表VS关联数组
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
5条回答 默认 最新
- dongzi4030 2010-06-28 16:40关注
In PHP, associative arrays are implemented as hashtables, with a bit of extra functionality.
However technically speaking, an associative array is not identical to a hashtable - it's simply implemented in part with a hashtable behind the scenes. Because most of its implementation is a hashtable, it can do everything a hashtable can - but it can do more, too.
For example, you can loop through an associative array using a for loop, which you can't do with a hashtable.
So while they're similar, an associative array can actually do a superset of what a hashtable can do - so they're not exactly the same thing. Think of it as hashtables plus extra functionality.
Code examples:
Using an associative array as a hashtable:
$favoriteColor = array(); $favoriteColor['bob']='blue'; $favoriteColor['Peter']='red'; $favoriteColor['Sally']='pink'; echo 'bob likes: '.$favoriteColor['bob']." "; echo 'Sally likes: '.$favoriteColor['Sally']." "; //output: bob likes blue // Sally likes pink
Looping through an associative array:
$idTable=array(); $idTable['Tyler']=1; $idTable['Bill']=20; $idTable['Marc']=4; //up until here, we're using the array as a hashtable. //now we loop through the array - you can't do this with a hashtable: foreach($idTable as $person=>$id) echo 'id: '.$id.' | person: '.$person." "; //output: id: 1 | person: Tyler // id: 20 | person: Bill // id: 4 | person: Marc
Note especially how in the second example, the order of each element is maintained (Tyler, Bill Marc) based on the order in which they were entered into the array. This is a major difference between associative arrays and hashtables. A hashtable maintains no connection between the items it holds, whereas a PHP associative array does (you can even sort a PHP associative array).
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
- ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
- ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
- ¥15 信息管理系统的查找和排序
- ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
- ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
- ¥15 PB中矩阵文本型数据的总计问题。
- ¥15 MATLAB卫星二体模型仿真
- ¥15 怎么让数码管亮的同时让led执行流水灯代码
- ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串