Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将向量传递给Lua表 < >我希望通过发送C++预格式化的LUA表: int GetCategory(lua_State* L) { uint32 Type = CHECKVAL<int>(L, 1); lua_newtable(L); int tbl = lua_gettop(L); uint32 counter = 1; // Struct CT { string CategoryBrandName, CategoryName }; > Vector<CT> auto list = sManagerMgr->GetAll(); // Hack modify this to send a metatable/UserData/Table whatever is called for (auto& elem : list) { switch (Type) { case 1: lua_pushstring(L, elem->CategoryBrandName); break; case 2: lua_pushstring(L, elem->CategoryName); break; } lua_rawseti(L, tbl, counter); counter++; } lua_settop(L, tbl); return 1; }_C++_Lua_Lua Table - Fatal编程技术网

将向量传递给Lua表 < >我希望通过发送C++预格式化的LUA表: int GetCategory(lua_State* L) { uint32 Type = CHECKVAL<int>(L, 1); lua_newtable(L); int tbl = lua_gettop(L); uint32 counter = 1; // Struct CT { string CategoryBrandName, CategoryName }; > Vector<CT> auto list = sManagerMgr->GetAll(); // Hack modify this to send a metatable/UserData/Table whatever is called for (auto& elem : list) { switch (Type) { case 1: lua_pushstring(L, elem->CategoryBrandName); break; case 2: lua_pushstring(L, elem->CategoryName); break; } lua_rawseti(L, tbl, counter); counter++; } lua_settop(L, tbl); return 1; }

将向量传递给Lua表 < >我希望通过发送C++预格式化的LUA表: int GetCategory(lua_State* L) { uint32 Type = CHECKVAL<int>(L, 1); lua_newtable(L); int tbl = lua_gettop(L); uint32 counter = 1; // Struct CT { string CategoryBrandName, CategoryName }; > Vector<CT> auto list = sManagerMgr->GetAll(); // Hack modify this to send a metatable/UserData/Table whatever is called for (auto& elem : list) { switch (Type) { case 1: lua_pushstring(L, elem->CategoryBrandName); break; case 2: lua_pushstring(L, elem->CategoryName); break; } lua_rawseti(L, tbl, counter); counter++; } lua_settop(L, tbl); return 1; },c++,lua,lua-table,C++,Lua,Lua Table,当前用途: print(i, Group(1)[i], Group(2)[i]); 所以。。我宁愿打一次电话,直接得到这样的东西: local Group = { [1] = { "elem->CategoryBrandName[1]", "elem->CategoryName[1]" }, [2] = { "elem->CategoryBrandName[2]", "elem->CategoryName[2]" }

当前用途:

print(i, Group(1)[i], Group(2)[i]);
所以。。我宁愿打一次电话,直接得到这样的东西:

local Group = 
{ 
        [1] = { "elem->CategoryBrandName[1]", "elem->CategoryName[1]" },
        [2] = { "elem->CategoryBrandName[2]", "elem->CategoryName[2]" }
        --etc
};
我曾尝试将元素填充到2D数组[1][2],然后推送数组失败

我已经做了很多关于表、元表、多维数组等的研究,但我找不到适合我需要或有效的东西


有人有解决方案吗?

为什么不让函数同时返回这两个值呢?那你就可以写了

local Group = { GetCategories }
我不是C API方面的专家,但我认为只要调用lua_Newtable就可以相当容易地做到这一点,所以类似这样:

int GetCategories(lua_State* L) {
  lua_settop(L, 0);
  // Discard arguments so we don't have to save the top of the stack
  // and can just use numbers instead (see following lines)
  lua_newtable(L); // Index 1 on stack
  lua_newtable(L); // Index 2 on stack

  // Do your magic

  lua_settop(L, 2); // Get rid of your temp variables
  return 2; // number of values we return in Lua
}
优化提示:您可以使用并告诉它每个表将有多少个元素,这样Lua就可以为它预先分配一些内存

编辑:我刚刚注意到这一点,但在您的代码中:

for (auto& elem : list) {
  switch (Type) {
  case 1:
    lua_pushstring(L, elem->CategoryBrandName);
    break;
  case 2:
    lua_pushstring(L, elem->CategoryName);
    break;
  }
  lua_rawseti(L, tbl, counter);
  counter++;
}
您只需不断将值推送到堆栈中。对于长向量,这可能会更快地溢出堆栈,从而导致麻烦。更好的方法是将1推到堆栈2插入到表3中,然后将其弹出:

// Modified for my suggested implementation that returns
// two tables. They can easily be turned around here.
for (auto& elem : list) {
  lua_pushstring(L, elem->CategoryBrandName);
  lua_rawseti(L, 1, counter++);
  lua_pop(L, 1);

  lua_pushstring(L, elem->CategoryName);
  lua_rawseti(L, 2, counter++);
  lua_pop(L, 1);
}
知道堆栈上有什么和没有什么总是一个好主意。节省一些内存不仅可以提高性能,还可以避免Lua堆栈溢出带来的潜在问题


最后一个细节:你不需要;在Lua中,除非在一行中有两条语句,否则使用它们被认为是不好的风格;打印“像这样”。

如果有人想要类似的东西,下面是我如何使用lua_createtable进行管理的。它按预期工作,可能需要一些改进想法

int GetCategory(lua_State* L)
    {   
        int counter = 1;
        int MaxListSize = 2;
        auto Categories = sManagerMgr->GetAll();

        lua_createtable(L, Categories.size(), 0);

        for (auto& elem : Categories)
        {
            vector<string> list;
            list.reserve(MaxListSize);

            list.emplace_back(elem->CategoryBrandName);
            list.emplace_back(elem->CategoryName);

            Macro::Push(L, counter); // custom
            lua_createtable(L, 0, MaxListSize);

            for (int i = 1; i <= MaxListSize; i++)
            {
                Macro::Push(L, list.at(i - 1)); // custom
                lua_rawseti(L, -2, i);
            }

            lua_settable(L, -3);
            list.clear();
            counter++;
        }
        return 1;
    }

lua_rawseti弹出正在从堆栈中推出的值,因此不需要显式弹出。@ratchetfreak啊,我不知道,谢谢你指出:rawseti上的计数器++每次都导致双增量。否则它就像一个符咒一样工作=啊,对不起,我先更改了它,然后复制了它xD
int GetCategory(lua_State* L)
    {   
        int counter = 1;
        int MaxListSize = 2;
        auto Categories = sManagerMgr->GetAll();

        lua_createtable(L, Categories.size(), 0);

        for (auto& elem : Categories)
        {
            vector<string> list;
            list.reserve(MaxListSize);

            list.emplace_back(elem->CategoryBrandName);
            list.emplace_back(elem->CategoryName);

            Macro::Push(L, counter); // custom
            lua_createtable(L, 0, MaxListSize);

            for (int i = 1; i <= MaxListSize; i++)
            {
                Macro::Push(L, list.at(i - 1)); // custom
                lua_rawseti(L, -2, i);
            }

            lua_settable(L, -3);
            list.clear();
            counter++;
        }
        return 1;
    }
local Group = 
{ 
        [1] = { "elem->CategoryBrandName[1]", "elem->CategoryName[2]" },
        [2] = { "elem->CategoryBrandName[1]", "elem->CategoryName[2]" }
        --etc
};