【xLua坑】使用#和table.count的区别

概念:

1、table是顺序表,即 { [1] = 1, [2] = 2, ...} 或 { 1, 2, ...} 

2、table是哈希表时,即 { ["a"] = 1, ["b"] = 2, ...} 或 { a = 1, b = 2 }

-- 计算哈希表长度
local function count(hashtable)
	local count = 0
	for _,_ in pairs(hashtable) do
		count = count + 1
	end
	return count
end

# 只能使用在顺序表

table.count 都可以! (table.count指上方的count方法)

问题往往出现在使用#来获取哈希表的长度,这个长度是无法准确获取到的!但,table.count做法肯定比#低效的.

猜你喜欢

转载自blog.csdn.net/qq_39574690/article/details/113419246