婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Lua中ipair和pair的區別

Lua中ipair和pair的區別

熱門標簽:武漢百應人工智能電銷機器人 400手機電話免費辦理 揚州電銷外呼系統軟件 上海企業外呼系統排名 百度地圖標注位置網站 如何利用高德地圖標注家 開通400電話申請流程 電腦外呼系統輻射大嗎 智能語音電銷的機器人

先看看官方手冊的說明吧:

復制代碼 代碼如下:

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會遍歷table的所有鍵值對。如果你看過耗子叔的Lua簡明教程,你知道table就是鍵值對的數據結構。

而ipairs就是固定地從key值1開始,下次key累加1進行遍歷,如果key對應的value不存在,就停止遍歷。順便說下,記憶也很簡單,帶i的就是根據integer key值從1開始遍歷的。

請看個例子。

復制代碼 代碼如下:

tb = {"oh", [3] = "god", "my", [5] = "hello", [6] = "world"}

for k,v in ipairs(tb) do
     print(k, v)
end


輸出結果就是:
復制代碼 代碼如下:

1       oh
2       my
3       god

因為tb不存在tb[4],所以遍歷到此為止了。
復制代碼 代碼如下:

for k,v in pairs(tb) do
     print(k, v)
end

輸出結果:
復制代碼 代碼如下:

1       oh
2       my
3       god
6       world
5       hello

我們都能猜到,將輸出所有的內容。然而你發現輸出的順序跟你tb中的順序不同。
如果我們要按順序輸出怎么辦?辦法之一是:
復制代碼 代碼如下:

for i = 1, #tb do
     if tb[i] then
          print(tb[i])
     else
end

當然,僅僅是個數組的話,ipairs也沒問題。

以上(為什么不少回答會以「以上」收尾?,這里就是結束的意思吧)

標簽:新余 江西 黑龍江 延邊 嘉峪關 武漢 宜賓 張掖

巨人網絡通訊聲明:本文標題《Lua中ipair和pair的區別》,本文關鍵詞  Lua,中,ipair,和,pair,的,區別,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Lua中ipair和pair的區別》相關的同類信息!
  • 本頁收集關于Lua中ipair和pair的區別的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 木兰县| 达孜县| 绍兴县| 门头沟区| 日土县| 甘泉县| 新源县| 全州县| 石台县| 皮山县| 上犹县| 安义县| 长汀县| 句容市| 武陟县| 碌曲县| 遂川县| 镇平县| 永顺县| 吉首市| 铜梁县| 易门县| 文登市| 阜阳市| 搜索| 香格里拉县| 贵州省| 南通市| 太仓市| 托里县| 天峨县| 海兴县| 石台县| 紫阳县| 伊宁县| 佛学| 合水县| 盱眙县| 栾城县| 玉屏| 博白县|