鍊錶簡單實現:
頭結點變數:list
插入操作:
list =
集合的實現:
用集合的元素作為引用放入到table
基本結構:
local reserved =
if reserved["white"] then
--訪問
end
完整過程:
local set= {}
function set:new(t)
local set = {}
for k,v in pairs(t) do
set[v] = (set[v] or 0) + 1
endsetmetatable(set, self)
self.__index = self
return set
endfunction set:insert(value)
self[value] = (self[value] or 0) + 1
endfunction set:remove(value)
local count = self[value]
self[value] = (count and count > 1) and count - 1 or nil
end
字串緩衝:
逐行讀取檔案:
local buff=""
for line in io.lines() do
buff=buff..line.."\n"
end
假如buff目前已經儲存了乙個50kb的字串,line每次讀取大小為20位元組。當執行**buff=buff..line.."\n"時,就會建立50020位元組的新字串,並從buff中複製至少50kb到新的字串。所以如此的話,沒讀取一行就會移動至少50kb的記憶體。在讀取了100行(2kb的字元)時,就至少移動了5mb的記憶體,所以這種做法在讀取較大檔案是不可取的。
所以很多語言提供了stringbuffer來解決這個問題。
在lua中用table在做類似的事:
local t={}
for line in io.lines() do
if(line==nil) then break end
t[#t+1]=line
endlocal s=table.concat(t,"\n") --將table t 中的字串連線起來
Lua 筆記 Lua 字串
目錄 lua 字串 字串操作 字串擷取 字串大小寫轉換 字串查詢與反轉 字串格式化 字元與整數相互轉換 案例 string1 lua print 字串 1 是 string1 string2 runoob.com print 字串 2 是 string2 string3 lua 教程 print 字...
字串鍊錶設計
標頭檔案 ifndef link h define link h typedef struct stringnode stringnode 1.向鍊錶尾部插入乙個元素 int tailinsertnode stringnode pnode,char ptaildata 2.遍歷 void print...
字串緩衝池
看到乙個關於字串緩衝池的討論 大家先來看看一段奇怪的程式 public class teststring 這個程式真是簡單啊!可是有什麼問題呢?1.來自 string 的憂慮 上面這段程式中,到底有幾個物件呢?可能很多人脫口而出 兩個,s1 和 s2 為什麼?string 是 final 類,它的值...