• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Lua中ipairs()和pairs()的区别与使用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

关于ipairs()和pairs(),Lua官方手册是这样说明的:

pairs (t)

If t has a metamethod __pairs, calls it with t as argument and returns the first three results from the call.

Otherwise, returns three values: the next function, the table t, and nil, so that the construction

     for k,v in pairs(t) do body end

will iterate over all key–value pairs of table t.

See function next for the caveats of modifying the table during its traversal.

ipairs (t)

If t has a metamethod __ipairs, calls it with t as argument and returns the first three results from the call.

Otherwise, returns three values: an iterator function, the table t, and 0, so that the construction

     for i,v in ipairs(t) do body end

will iterate over the pairs (1,t[1]), (2,t[2]), ..., up to the first integer key absent from the table.

根据官方手册的描述,pairs会遍历表中所有的key-value值,而pairs会根据key的数值从1开始加1递增遍历对应的table[i]值,直到出现第一个不是按1递增的数值时候退出。

下面我们以例子说明一下吧

stars = {[1] = "Sun", [2] = "Moon", [5] = 'Earth'}

for i, v in pairs(stars) do

   print(i, v)

end

使用pairs()将会遍历表中所有的数据,输出结果是:

1    Sun
2    Moon
5    Earth

如果使用ipairs()的话,

for i, v in ipairs(stars) do

   print(i, v)

end

当i的值遍历到第三个元素时,i的值为5,此时i并不是上一个次i值(2)的+1递增,所以遍历结束,结果则会是:

1    Sun
2    Moon

ipairs()和pairs()的区别就是这么简单。

还有一个要注意的是pairs()的一个问题,用pairs()遍历是[key]-[value]形式的表是随机的,跟key的哈希值有关系。看以下这个例子:

stars = {[1] = "Sun", [2] = "Moon", [3] = "Earth", [4] = "Mars", [5] = "Venus"}

for i, v in pairs(stars) do

   print(i, v)

end

结果是:

2    Moon
3    Earth
1    Sun
4    Mars
5    Venus

并没有按照其在表中的顺序输出。

但是如果是这样定义表stars的话

stars = {"Sun", "Moon",  “Earth”, "Mars",  "Venus"}

结果则会是

1    Sun
2    Moon
3    Earth
4    Mars
5    Venus

你清楚了吗?:)

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Lua架构TheLuaArchitecture发布时间:2022-07-22
下一篇:
unitylua热更新之利用lua的table实现c#的new对象发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap