sta :: map在Lua中的实现类型

时间:2016-07-18 17:21:16

标签: dictionary lua lua-table

我有<h3 class="svgbg">ORGANIZERS</h3><!--name of the first category--> <?php foreach($category as $img) :?> <div> <?php echo "Category Name: ".$img['title'];?> <br> <?php foreach($img['r_images'] as $imgage) :?> <div class="thumbnail"> <?=img($imgage->file)?> <br> </div> <?php endforeach; ?> </div> <?php endforeach; ?> ,其中包含与Key关联的值列表。实际实现包含许多此类密钥。在Lua Table实现中是否有类似的方法可以为特定键保存多个值。如果是这样,如何从该表中写入和读取。

我提到How do I create a Lua Table in C++, and pass it to a Lua function? 我只能访问我的C ++代码中的set和get值,这些代码写得更通用,无法在C ++中创建表。 (第三方C ++代码)。

我只能使用

获取KeyType,Key和Value

std::map

C ++代码类似于

luaState = luaL_newstate();
 lua_register(luaState, "getValue", get_value);
 lua_register(luaState, "setValue", set_value);

通过引用Lua文档,我了解到我可以使用Key作为索引创建一个表,并将值指定为其数据。但我需要知道如何为一个Key分配多个值(数据)。 https://www.lua.org/pil/2.5.html

示例lua脚本实现就像,

typedef std::set<const char *> TValueNames;
std::map<const char *, TValueNames> keyValueList;

我需要创建一些可以保存信息的内容,例如

local keyType = getValue("KeyType");
local Key = getValue("Key");
local Value = getValue("Value");
KeyValueTable = {}
KeyValueTable[Key] = Value;

1 个答案:

答案 0 :(得分:3)

如您所知,Lua表中的键只能引用一个值,但您可以轻松地将该值作为表来保存多个值。为了更忠实地表示C ++结构中的set,我们可以将值设置为内部表中的键。

local function setValue(self, key, value)
  self[key] = self[key] or {}
  self[key][value] = true
end

local function removeValue(self, key, value)
  if type(self[key]) == 'table' then
    self[key][value] = nil
  end
end

local function checkValue(self, key, value)
  if type(self[key]) == 'table' then
    return self[key][value]
  end
end
相关问题