Lua的table库函数insert、remove、concat、sort详细介绍(转载)
函数列表:
table.insert(table,[ pos,] value)
table.remove(table[, pos])
table.concat(table[, sep[, i[, j]]])
table.sort(table[, comp])
insert 和 remove 只能用于数组元素的插入和移出, 进行插入和移出时,会将后面的元素对齐起来。
所以在 for 循环中进行 insert 和 remove 的时候要注意插入和移除时是否漏掉了某些项:
,,,,,,}
for i,v in ipairs(t) do
then
table.remove(t,i)
end
end
-- 错误,第四个 3 没有被移除,ipairs 内部会维护一个变量记录遍历的位置,remove 掉第三个数字 3 之后,ipairs 下一个返回的值是 5 而不是 3
,,,,,,}
, #t do
then
table.remove(t,i)
i = i-
end
end
-- 错误,i=i-1 这段代码没有用,i 的值始终是从 1 到 #t,for 循环里修改 i 的值不起作用
,,,,,,}
, - do
then
table.remove(t,i)
end
end
-- 正确,从后往前遍历
,,,,,,}
while t[i] do
then
table.remove(t,i)
else
i = i+
end
end
-- 正确,自己控制 i 的值是否增加
concat 可以将 table 的数组部分拼接成一个字符串,中间用 seq 分隔。
lua 中字符串的存储方式与 C 不一样,lua 中的每个字符串都是单独的一个拷贝,拼接两个字符串会产生一个新的拷贝,如果拼接操作特别多,就会影响性能:
local beginTime = os.clock()
local str = ""
, do
str = str .. i
end
local endTime = os.clock()
print(endTime - beginTime)
-- 消耗 0.613 秒,产生了 30000 个字符串拷贝,但只有最后一个是有用的
local beginTime = os.clock()
local t = {}
, do
t[i] = i
end
local str = table.concat(t, "")
local endTime = os.clock()
print(endTime - beginTime)
-- 消耗 0.024 秒,利用 concat,一次性把字符串拼接出来,只产生了一个字符串拷贝
sort 可以将 table 数组部分的元素进行排序,需要提供 comp 函数,comp(a, b) 如果 a 应该排到 b 前面,则 comp 要返回 true 。
注意,对于 a==b 的情况,一定要返回 false :
local function comp(a,b)
return a <= b
end
table.sort(t,comp)
-- 错误,可能出现异常:attempt to compare number with nil
local function comp(a,b)
if a == nil or b == nil then
return false
end
return a <= b
end
table.sort(t,comp)
-- 错误,可能出现异常:invalid order function for sorting
-- 也可能不报这个异常,但结果是错误的;
之所以 a==b 返回true 会引发这些问题,是因为 table.sort 在实现快速排序时没有做边界检测:
for (;;) {
, ++i), sort_comp(L, -, -)) { // 未检测边界, i 会一直增加
if (i>=u) luaL_error(L, "invalid order function for sorting");
lua_pop(L, );
}
, --j), sort_comp(L, -3, -1)) { // 未检测边界, j 会一直减少
if (j<=l) luaL_error(L, "invalid order function for sorting");
lua_pop(L, );
}
if (j<i) {
lua_pop(L, );
break;
}
set2(L, i, j);
}
看以上代码,如果 a==b 时返回 true 且边界上的几个值是相等的话, sort_comp 就无法阻止 i 继续增长,直到超出边界引发异常 attempt to compare number with nil;即使我们对 a 和 b 进行非空判断,也会因为 i 超过边界而引发异常 invalid order function for sorting
Lua的table库函数insert、remove、concat、sort详细介绍(转载)的更多相关文章
- (转)Lua的table库函数insert、remove、concat、sort详细介绍
原帖链接:http://www.jb51.net/article/64711.htm#comments 有增注标识的地方为额外注释,非原帖内容. 函数列表:(增注:只能用于数组!) table.ins ...
- js中的数组对象排序(方法sort()详细介绍)
定义和用法 sort() 方法用于对数组的元素进行排序. 语法 arrayObject.sort(sortby) 参数sortby:可选.规定排序顺序.必须是函数. 返回值 对数组的引用.请注意 ...
- Lua 之table库
标准table库 table.concat(table, sep, start, end) concat是concatenate(连锁, 连接)的缩写,table.concat()函数列出参数中指定 ...
- lua的table库中经常使用的函数
lua提供了一些辅助函数来操作table. 比如,从list中insert和remove元素,对array的元素进行sort.或者concatenate数组中的全部strings.以下就具体地解说这些 ...
- Lua之table
Lua table(表) 参考:http://www.runoob.com/lua/lua-tables.html table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数字.字典 ...
- lua 12 table 的使用
转自:http://www.runoob.com/lua/lua-tables.html table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table ...
- lua的table库中的常用函数总结
table是Lua语言中的一种重要的数据类型, table 的一些特性简单列举如下: (1).table 是一个“关联数组”,数组的索引可以是数字或者是字符串; (2).table 的默认初始索引一般 ...
- lua的table表去重
推荐阅读: 我的CSDN 我的博客园 QQ群:704621321 我的个人博客 方法一 用过lua的人都知道,lua的table中不允许存在相同的key,利用这个思想,我们可以将原始table ...
- lua的table表处理 及注意事项
lua,一款很轻量级很nice很强大的脚本语言,做为lua中使用最为频繁的table表,在使用之时还是有颇多的好处与坑的: 下面是大牛 云风的一片关于lua table的blog,可使得对lua ta ...
随机推荐
- python入门(十):XML和JSON解析
一.python解析XML 1.xml.dom.*模块,它是W3C DOM API的实现,若需要处理DOM API则该模块很适合,注意xml.dom包里面有许多模块,须区分它们间的不同: 2.xml. ...
- 自定义控件_StickyNavLaout
关注我一.View结构原理1.extends linearLayout 继承想要用的布局,首先完成布局的填充在 onFinishInflate 方法中 findViewById(); @Overrid ...
- IOS 程序内部切换语言 的一种方法
本文转载至 http://www.cnblogs.com/wuyijibei/archive/2013/08/01/3230468.html 1: 首先, 所有的语言资源还是需要和现在的i18n方法 ...
- deviceready has not fired after 5 seconds
deviceready has not fired after 5 seconds 建议用手机连上电脑,用真机进行调试:
- maven打包排除spring-boot内嵌tomcat容器依赖jar
在pom文件中添加打包排除配置信息. <plugin> <artifactId>maven-war-plugin</artifactId> <version& ...
- Oracle涂抹oracle学习笔记第10章Data Guard说,我就是备份
DG 是备份恢复工具,但是更加严格的意义它是灾难恢复 Data Guard是一个集合,由一个Primary数据库及一个或者多个Standby数据库组成,分两类逻辑Standby和物理Standby 1 ...
- Code Forces 644A Parliament of Berland
A. Parliament of Berland time limit per test1 second memory limit per test256 megabytes inputstandar ...
- R中,将从MySQL中获取的结果字符列表转化为向量,并测试绘制图形
# 使用RMySQL操作数据库 # 载入DBI和RMySQL包 library(DBI) library(RMySQL) # 创建数据库连接 con <- dbConnect(MySQL(),h ...
- android开发时程序出现崩溃问题
原因:项目目录下的:src与gen中的包名不一致时,出现问题 <manifest xmlns:android="http://schemas.android.com/apk/res/a ...
- 不阻塞浏览器的解析,待外部js下载完成后异步执行
网站统计中的数据收集原理及实现(js埋点实现) - lastwhisper - CSDN博客 https://blog.csdn.net/l1212xiao/article/details/80450 ...