返回C+中的映射+;在lua中用作桌子 我希望能够在C++中使用Lua返回一个值表,而不是USER数据、存储在地图中的数据,如整数、字符串等。我该怎么做呢
对不起,我没有完整的例子 这是地图返回C+中的映射+;在lua中用作桌子 我希望能够在C++中使用Lua返回一个值表,而不是USER数据、存储在地图中的数据,如整数、字符串等。我该怎么做呢,c++,dictionary,lua,C++,Dictionary,Lua,对不起,我没有完整的例子 这是地图 std::map<uint16_t, std::map<std::string, uint32_t>> myMap; void getMyMap(uint16_t i, std::string a, uint32_t& v) { v = myMap[i][a]; } std::map myMap; void getMyMap(uint16\u t i,std::string a,uin
std::map<uint16_t, std::map<std::string, uint32_t>> myMap;
void getMyMap(uint16_t i, std::string a, uint32_t& v) {
v = myMap[i][a];
}
std::map myMap;
void getMyMap(uint16\u t i,std::string a,uint32\u t&v){
v=myMap[i][a];
}
我知道这不是一个模板,但假设它是
编辑:
我自己想出来的。我不会给你确切的我的工作,但我会提供一个通用的详细答案,使用上述代码铭记
std::map<uint16_t, std::map<std::string, uint32_t>> myMap;
void getMyMap(uint16_t i, std::string a, uint32_t& v) {
v = myMap[i][a];
}
std::map<std::string, size_t> getMapData(uint16_t i){
return myMap[i];
}
int LuaReturnTableOfMap(lua_State *L)
{
uint16_t data = 2;// used just for this example
// get the data stored
std::map<std::string, size_t> m = getMapData(data);
// get the size of the map
size_t x = m.size();
// create the table which we will be returning with x amount of elements
lua_createtable(L, 0, x);
// populate the table with values which we want to return
for(auto const &it : m){
std::string str = it.first;
const char* field = str.c_str();
lua_pushnumber(L, it.second);
lua_setField(L, -2, field);
}
// tell lua we are returning 1 value (which is the table)
return 1;
}
std::map myMap;
void getMyMap(uint16\u t i,std::string a,uint32\u t&v){
v=myMap[i][a];
}
std::map getMapData(uint16\u t i){
返回myMap[i];
}
地图的内部lua可转台(lua_州*L)
{
uint16_t data=2;//仅用于此示例
//获取存储的数据
std::map m=getMapData(数据);
//获取地图的大小
尺寸x=m.尺寸();
//创建表,我们将返回x个元素
lua_创建表(L,0,x);
//使用要返回的值填充表
用于(自动常数和it:m){
std::string str=it.first;
常量字符*字段=str.c_str();
lua_pushnumber(L,it.second);
lua_设置字段(L,-2,字段);
}
//告诉lua我们返回1个值(即表)
返回1;
}
对于C++/Lua,我无法推荐足够的Sol2()
在C++中,你可以分配嵌套的映射,并且足够聪明地为你创建嵌套的LUA表:
std::map<int, std::map<std::string, int>> nestedmap;
std::map<std::string, int> innermap;
innermap["frank"] = 15;
nestedmap[2] = innermap;
sol::state lua;
lua.set("mymap", nestedmap);
谢谢你的回答,但我已经在使用lua的框架,只是我不确定如何返回地图或向量。你的例子对我帮助不大。对不起,我想我不确定你的问题。您是否试图返回一个STD::从C++函数映射?
print("Testing nested map: " .. mymap[2]["frank"]) -- prints 15