主頁(yè) > 知識(shí)庫(kù) > Lua的table庫(kù)函數(shù)insert、remove、concat、sort詳細(xì)介紹

Lua的table庫(kù)函數(shù)insert、remove、concat、sort詳細(xì)介紹

熱門(mén)標(biāo)簽:百度地圖標(biāo)注早餐區(qū)域 漳州智云呼電話機(jī)器人 冀州市地圖標(biāo)注 清朝地圖標(biāo)注哈爾濱 地圖標(biāo)注大廈 怎么去除地圖標(biāo)注 新岸線智能電銷機(jī)器人 武漢外呼防封系統(tǒng)多少錢(qián) 個(gè)人怎么在地圖標(biāo)注需要的店鋪

函數(shù)列表:

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

1. insert 和 remove 只能用于數(shù)組元素的插入和移出, 進(jìn)行插入和移出時(shí),會(huì)將后面的元素對(duì)齊起來(lái)。

    所以在 for 循環(huán)中進(jìn)行 insert 和 remove 的時(shí)候要注意插入和移除時(shí)是否漏掉了某些項(xiàng):
 

復(fù)制代碼 代碼如下:

        local t = {1,2,3,3,5,3,6}
        for i,v in ipairs(t) do
            if v == 3 then
                table.remove(t,i)
            end
        end
        -- 錯(cuò)誤,第四個(gè) 3 沒(méi)有被移除,ipairs 內(nèi)部會(huì)維護(hù)一個(gè)變量記錄遍歷的位置,remove 掉第三個(gè)數(shù)字 3 之后,ipairs 下一個(gè)返回的值是 5 而不是 3
       
        local t = {1,2,3,3,5,3,6}
        for i=1, #t do
            if t[i] == 3 then
                table.remove(t,i)
                i = i-1
            end
        end
        -- 錯(cuò)誤,i=i-1 這段代碼沒(méi)有用,i 的值始終是從 1 到 #t,for 循環(huán)里修改 i 的值不起作用
       
        local t = {1,2,3,3,5,3,6}
        for i=#t, 1, -1 do
            if t[i] == 3 then
                table.remove(t,i)
            end
        end
        -- 正確,從后往前遍歷
       
        local t = {1,2,3,3,5,3,6}
        local i = 1
        while t[i] do
            if t[i] == 3 then
                table.remove(t,i)
            else
                i = i+1
            end
        end
        -- 正確,自己控制 i 的值是否增加
    

   2. concat 可以將 table 的數(shù)組部分拼接成一個(gè)字符串,中間用 seq 分隔。 
    lua 中字符串的存儲(chǔ)方式與 C 不一樣,lua 中的每個(gè)字符串都是單獨(dú)的一個(gè)拷貝,拼接兩個(gè)字符串會(huì)產(chǎn)生一個(gè)新的拷貝,如果拼接操作特別多,就會(huì)影響性能:
 

復(fù)制代碼 代碼如下:

        local beginTime = os.clock()
        local str = ""
        for i=1, 30000 do
            str = str .. i
        end
        local endTime = os.clock()
        print(endTime - beginTime)
        -- 消耗 0.613 秒,產(chǎn)生了 30000 個(gè)字符串拷貝,但只有最后一個(gè)是有用的

        local beginTime = os.clock()
        local t = {}
        for i=1, 30000 do
            t[i] = i
        end
        local str = table.concat(t, "")
        local endTime = os.clock()
        print(endTime - beginTime)
        -- 消耗 0.024 秒,利用 concat,一次性把字符串拼接出來(lái),只產(chǎn)生了一個(gè)字符串拷貝
      

3. sort 可以將 table 數(shù)組部分的元素進(jìn)行排序,需要提供 comp 函數(shù),comp(a, b) 如果 a 應(yīng)該排到 b 前面,則 comp 要返回 true 。    
    注意,對(duì)于 a==b 的情況,一定要返回 false :
 

復(fù)制代碼 代碼如下:

        local function comp(a,b)
            return a = b
        end
        table.sort(t,comp)
        -- 錯(cuò)誤,可能出現(xiàn)異常: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)
        -- 錯(cuò)誤,可能出現(xiàn)異常:invalid order function for sorting
        -- 也可能不報(bào)這個(gè)異常,但結(jié)果是錯(cuò)誤的;
    之所以 a==b 返回true 會(huì)引發(fā)這些問(wèn)題,是因?yàn)?table.sort 在實(shí)現(xiàn)快速排序時(shí)沒(méi)有做邊界檢測(cè):
        for (;;) {
          while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {  // 未檢測(cè)邊界, i 會(huì)一直增加
            if (i>=u) luaL_error(L, "invalid order function for sorting");
            lua_pop(L, 1);
          }
          while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {  // 未檢測(cè)邊界, j 會(huì)一直減少
            if (j=l) luaL_error(L, "invalid order function for sorting");
            lua_pop(L, 1);
          }
          if (ji) {
            lua_pop(L, 3);
            break;
          }
          set2(L, i, j);
        }
  

    看以上代碼,如果 a==b 時(shí)返回 true 且邊界上的幾個(gè)值是相等的話, sort_comp 就無(wú)法阻止 i 繼續(xù)增長(zhǎng),直到超出邊界引發(fā)異常 attempt to compare number with nil;即使我們對(duì) a 和 b 進(jìn)行非空判斷,也會(huì)因?yàn)?i 超過(guò)邊界而引發(fā)異常 invalid order function for sorting
    快速排序是什么,lua 如何實(shí)現(xiàn)快速排序,可以參考 lua 源碼中的描述,這里不多介紹;

您可能感興趣的文章:
  • 深入談?wù)刲ua中神奇的table
  • Lua Table轉(zhuǎn)C# Dictionary的方法示例
  • Lua中設(shè)置table為只讀屬性的方法詳解
  • Lua編程示例(一):select、debug、可變參數(shù)、table操作、error
  • 舉例講解Lua中的Table數(shù)據(jù)結(jié)構(gòu)
  • Lua table中安全移除元素的方法
  • C++遍歷Lua table的方法實(shí)例
  • Lua中釋放table占用內(nèi)存的方法
  • Lua中table的遍歷詳解
  • Lua中獲取table長(zhǎng)度問(wèn)題探討
  • Lua中獲取table長(zhǎng)度的方法
  • Lua中table里內(nèi)嵌table的例子
  • Lua面向?qū)ο缶幊讨A(chǔ)結(jié)構(gòu)table簡(jiǎn)例

標(biāo)簽:濰坊 天門(mén) 天門(mén) 德宏 金昌 臺(tái)灣 宣城 儋州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Lua的table庫(kù)函數(shù)insert、remove、concat、sort詳細(xì)介紹》,本文關(guān)鍵詞  Lua,的,table,庫(kù),函數(shù),insert,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Lua的table庫(kù)函數(shù)insert、remove、concat、sort詳細(xì)介紹》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Lua的table庫(kù)函數(shù)insert、remove、concat、sort詳細(xì)介紹的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章