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 ...
随机推荐
- python3----requests
import requests def get_html_text(url): try: r = requests.get(url, timeout=30) r.raise_for_status() ...
- iOS 模块化
模块化 1.公共模块 网络层 模型层(基类) 2.mvvm 3.模块化(单元模块,实现单元功能,单元测试) 4.pod 5.路由
- Ubuntu 编译安装搭配LNMP 环境
这里用Nginx1.2.0+mysql5.6.33+php5.6.2搭配安装环境 ---------------------------------------------Nginx BEGIN--- ...
- 编程之美 最长递增子序列 LIS
1. O(N*logN) 解法 先对序列排序, 然后寻找两个序列的最长公共子序列 2. O(N*N) 的动态规划解法 令 LIST[i] 表示以 i 为结尾的最长子序列的长度, 那么 LIST[J] ...
- ios常用第三方库git下载地址
本文转载至 http://blog.csdn.net/cerastes/article/details/38348599 iOS第三方库下载常用git 1.FMDB https://github.co ...
- chkcofnig-minimal-script
author :headsen chen date: 2018-06-04 12:02:27 #!/bin/bash for i in `chkconfig --list |awk '{print ...
- Linux上查看和修改字符集
author :headsen chen date: 2018-05-14 16:20:30 一·查看字符集 字符集在系统中体现形式是一个环境变量,看当前终端使用字符集的有以下几种方式: 1: 1 ...
- 【BZOJ2792】[Poi2012]Well 二分+双指针法
[BZOJ2792][Poi2012]Well Description 给出n个正整数X1,X2,...Xn,可以进行不超过m次操作,每次操作选择一个非零的Xi,并将它减一. 最终要求存在某个k满足X ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- 2018-2019-1 20165330 《信息安全系统设计基础》第六周课上测试ch02&课下作业
课上测试 测试-3-ch02 任务详情 编写一个程序 "week0203学号.c",运行下面代码: 1 short int v = -学号后四位 2 unsigned short ...