2

我想从 Lua 代码中调用一些 C++ 函数并在参数中传递 Lua 表,如下所示:

call_cpp_function_from_lua({ x = 10, y = 20 })

call_cpp_function_from_lua我想获取并使用迭代器来表示 Lua 表的 C++,如下所示:

std::map<boost::variant<LuaTypesList>, boost::variant<LuaTypesList>>::iterator it = getLuaTableBegin();

我可以使用 C API 来执行此操作,但它很乏味且容易出错,请参阅Iterating through a Lua table from C++? .

4

1 回答 1

1

QtLua 库为 Lua 表实现了 C++ 迭代器。它有Value::iteratorValue::const_iterator类,允许 Lua 表的迭代。以下是有关如何使用它们的简短示例:

// code from examples/cpp/value/iterate.cc:32

    QtLua::State state;

    // New lua table value
    state.exec_statements("table = { a = 1, b = 2, c = 3 }");

    QtLua::Value table = state["table"];

    // Iterate over lua table from C++ code
    for (QtLua::Value::const_iterator i = table.begin(); i != table.end(); i++)
      qDebug() << i.key().to_string_p()
               << i.value().to_string_p();
于 2013-03-10T10:57:33.200 回答