好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Lua中ipair和pair的區(qū)別

Lua中ipair和pair的區(qū)別

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

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

復制代碼 代碼如下:

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就是鍵值對的數(shù)據(jù)結構。

而ipairs就是固定地從key值1開始,下次key累加1進行遍歷,如果key對應的value不存在,就停止遍歷。順便說下,記憶也很簡單,帶i的就是根據(jù)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

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

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

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

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

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

巨人網(wǎng)絡通訊聲明:本文標題《Lua中ipair和pair的區(qū)別》,本文關鍵詞  Lua,中,ipair,和,pair,的,區(qū)別,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Lua中ipair和pair的區(qū)別》相關的同類信息!
  • 本頁收集關于Lua中ipair和pair的區(qū)別的相關信息資訊供網(wǎng)民參考!
  • 推薦文章