原帖链接:http://www.jb51.net/article/64711.htm#comments

有增注标识的地方为额外注释,非原帖内容。

函数列表:(增注:只能用于数组!)

table.insert(table,[ pos,] value)
table.remove(table[, pos])
table.concat(table[, sep[, i[, j]]])
table.sort(table[, comp])

1. insert 和 remove 只能用于数组元素的插入和移出, 进行插入和移出时,会将后面的元素对齐起来。

(增注:和C++里对std::vector等容器使用iterator迭代器进行删除类似)

所以在 for 循环中进行 insert 和 remove 的时候要注意插入和移除时是否漏掉了某些项:

        local t = {,,,,,,} 

        for i,v in ipairs(t) do 

            if v ==  then 

                table.remove(t,i) 

            end 

        end 

        -- 错误,第四个 3 没有被移除,ipairs 内部会维护一个变量记录遍历的位置,remove 掉第三个数字 3 之后,ipairs 下一个返回的值是 5 而不是 3 

        

        local t = {,,,,,,} 

        for i=, #t do 

            if t[i] ==  then 

                table.remove(t,i) 

                i = i- 

            end 

        end 

        -- 错误,i=i-1 这段代码没有用,i 的值始终是从 1 到 #t,for 循环里修改 i 的值不起作用 

        

        local t = {,,,,,,} 

        for i=#t, , - do 

            if t[i] ==  then 

                table.remove(t,i) 

            end 

        end 

        -- 正确,从后往前遍历 

        

        local t = {,,,,,,} 

        local i =  

        while t[i] do 

            if t[i] ==  then 

                table.remove(t,i) 

            else 

                i = i+ 

            end 

        end 

        -- 正确,自己控制 i 的值是否增加

2. concat 可以将 table 的数组部分拼接成一个字符串,中间用 seq 分隔。

(增注:对字符串的操作,一定要避免使用..符号!可以使用string.format进行操作,性能上比..要高得多)
    lua 中字符串的存储方式与 C 不一样,lua 中的每个字符串都是单独的一个拷贝,拼接两个字符串会产生一个新的拷贝,如果拼接操作特别多,就会影响性能

        local beginTime = os.clock() 

        local str = "" 

        for i=,  do 

            str = str .. i 

        end 

        local endTime = os.clock() 

        print(endTime - beginTime) 

        -- 消耗 0.613 秒,产生了 30000 个字符串拷贝,但只有最后一个是有用的
        local beginTime = os.clock()         local t = {}         for i=, do             t[i] = i         end         local str = table.concat(t, "")         local endTime = os.clock()         print(endTime - beginTime)         -- 消耗 0.024 秒,利用 concat,一次性把字符串拼接出来,只产生了一个字符串拷贝

3. 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 (;;) { 

          while (lua_rawgeti(L, , ++i), sort_comp(L, -, -)) {  // 未检测边界, i 会一直增加 

            if (i>=u) luaL_error(L, "invalid order function for sorting"); 

            lua_pop(L, ); 

          } 

          while (lua_rawgeti(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); 

        } 

增注:下面为正确的写法(这里省略了b == nil)

        local function comp(a,b) 

            if a == nil or a == b then 

                return false 

            end 

            return a < b 

        end 

        table.sort(t,comp) 

(转)Lua的table库函数insert、remove、concat、sort详细介绍的更多相关文章

  1. Lua的table库函数insert、remove、concat、sort详细介绍(转载)

    函数列表: table.insert(table,[ pos,] value) table.remove(table[, pos]) table.concat(table[, sep[, i[, j] ...

  2. Lua 之table库

    标准table库 table.concat(table, sep,  start, end) concat是concatenate(连锁, 连接)的缩写,table.concat()函数列出参数中指定 ...

  3. lua的table库中经常使用的函数

    lua提供了一些辅助函数来操作table. 比如,从list中insert和remove元素,对array的元素进行sort.或者concatenate数组中的全部strings.以下就具体地解说这些 ...

  4. Lua之table

    Lua table(表) 参考:http://www.runoob.com/lua/lua-tables.html table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数字.字典 ...

  5. lua 12 table 的使用

    转自:http://www.runoob.com/lua/lua-tables.html table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table ...

  6. lua的table库中的常用函数总结

    table是Lua语言中的一种重要的数据类型, table 的一些特性简单列举如下: (1).table 是一个“关联数组”,数组的索引可以是数字或者是字符串; (2).table 的默认初始索引一般 ...

  7. lua的table表去重

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321  我的个人博客 方法一 用过lua的人都知道,lua的table中不允许存在相同的key,利用这个思想,我们可以将原始table ...

  8. lua的table表处理 及注意事项

    lua,一款很轻量级很nice很强大的脚本语言,做为lua中使用最为频繁的table表,在使用之时还是有颇多的好处与坑的: 下面是大牛 云风的一片关于lua table的blog,可使得对lua ta ...

  9. lua中 table 元表中元方法的重构实现

    转载请标明出处http://www.cnblogs.com/zblade/ lua作为游戏的热更新首选的脚本,其优势不再过多的赘述.今天,我主要写一下如何重写lua中的元方法,通过自己的重写来实现对l ...

随机推荐

  1. Linux centos7下php安装cphalcon扩展的方法

    说明: 操作系统:CentOS7 php安装目录:/usr/local/php php.ini配置文件路径:/usr/local/php/etc/php.ini 运行环境:LNMP ,PHP7 .安装 ...

  2. Golang并发编程中select简单了解

    select可以监听channel的数据流动select的用法与switch语法非常类似,由select开始的一个新的选择块,每个选择条件由case语句来描述 与switch语句可以选择任何使用相等比 ...

  3. PyCharm‘s Project Deployment

    当在本地写完项目,部署到服务器上调试的时候,难免会碰到代码的修修改改,但由于项目在服务器上,修改起来相对麻烦.各路大神或许有自己的方法去解决.这篇博客演示利用PyCharm的Deployment功能, ...

  4. P2698 [USACO12MAR]花盆Flowerpot(单调队列+二分)

    P2698 [USACO12MAR]花盆Flowerpot 一看标签........十分后悔 标签告诉你单调队列+二分了............ 每次二分花盆长度,蓝后开2个单调队列维护最大最小值 蓝 ...

  5. Python3 离线安装TensorFlow包

    Python3 离线安装TensorFlow包 1,下载包 官网地址:https://pypi.org/project/tensorflow/1.1.0rc2/#files 清华镜像:https:// ...

  6. 复制MIFARE Classic卡

    Mifare Classic 1K智能卡介绍及nfc-tools的使用 [原创]RFID安全之——ACR122U菜鸟初体验-『智能设备』-看雪安全论坛 复制MIFARE Classic小区门禁卡记录 ...

  7. C# 文件与二进制之间的转换

    /// <summary> /// 工具类:文件与二进制流间的转换 /// </summary> public class FileBinaryConvertHelper { ...

  8. Python常用库之functools

    functools 是python2.5被引人的,一些工具函数放在此包里. python2.7中 python3.6中 import functools print(dir(functools)) [ ...

  9. POJ 2387 Til the Cows Come Home 【最短路SPFA】

    Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...

  10. bzoj 1251: 序列终结者 平衡树,fhqtreap

    链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1251 思路 好简单的模板题 不过还是wrong了好几发 叶子节点要注意下,不能使用 遇到就不 ...