首页 > 其他 > 详细

lua 4 使用table实现其他数据结构,并介绍遍历方法

时间:2018-11-11 18:17:04      阅读:172      评论:0      收藏:0      [点我收藏+]

本文会以vector / map / set 这三种数据类型的角度来梳理 table 支持的不同遍历方式。

 

table as array / vector

一般,C/C++中的 array / vector (下文简称 vector) 是没有 key。但是在 lua 中使用了 table 这种通用结构,就引入了 key 的问题。

在这里,把想用做 vector 的 table,做一个非常重要的约定:1 初始,key 连续。由于 table 的自由度很高,这个需要开发者自己约束。

---- 新建

t = {"A", "BB", "CCC"} -- 默认的key就是1初始且连续

-- 或者
t = {} -- 建立一个空的
t[1] = 1
t[2] = 2
t[3] = 3

-- 或者
t = {  
    [1] = 1,  
    [2] = 2,  
    [3] = 3,  -- 这里逗号可有可无
}

  

---- 赋值

 

---- 遍历

 

 

 

table as map / linked list

 

table as set

 

table as queues (队列,先进先出,不做介绍,请参考原文)

请参考:https://www.lua.org/pil/11.4.html

 

参考

http://blog.51cto.com/rangercyh/1032925

https://www.lua.org/pil/11.html

 

lua 4 使用table实现其他数据结构,并介绍遍历方法

原文:https://www.cnblogs.com/alexYuin/p/9942842.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!