c++ - 如何在 C++ 中创建 Lua 表,并将其传递给 Lua 函数?

标签 c++ string map lua lua-table

在 C++ 中,我有一个 map<string, string> ,包含未知数量的条目。我如何将其传递给 Lua 函数,以便 Lua 函数可以将数据用作表格?

最佳答案

如果你想要一个真正的lua表:

lua_newtable(L);
int top = lua_gettop(L);

for (std::map::iterator it = mymap.begin(); it != mymap.end(); ++it) {
    const char* key = it->first.c_str();
    const char* value = it->second.c_str();
    lua_pushlstring(L, key, it->first.size());
    lua_pushlstring(L, value, it->second.size());
    lua_settable(L, top);
}

将正确的 map 类型替换为......

关于c++ - 如何在 C++ 中创建 Lua 表,并将其传递给 Lua 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/453769/

相关文章:

c++ - 创建相似对象时减少内存浪费

c++ - integral_constant 与 constexpr

c++ - C/C++ Paypal 集成

arrays - 是否可以在数组中快速访问数组中的字符串?

c# - 将字符串转换为日期时间返回最小值

c++ - 函数指针映射。被指向的函数是否必须是静态的?

c++ - 交换算法赋予所有相同的值

c# - 如何将 JSON 值分配给字符串变量

Golang 中的 map 集

map - 在 Clojure 中切片 map 的惯用方法是什么?