c/c++_Lua交互----关于Lua中table类型的使用实例
2014-12-06 14:45
429 查看
lua中的复合类型 只有table 类型,你可以当做任意容器使用 ,比如 数组 PHP中的关联数组 C++中的 std::map 等等 而且提供了很方便的使用
下面是lua中 table类型的使用
c++加载代码
Lua脚本代码
最后界面
下面是lua中 table类型的使用
c++加载代码
#include "string.h" extern "C" { #include "lualib.h" //包含lua lib #include "lauxlib.h" //辅助函数 }; #pragma comment(lib,"lua.lib") //Lua和C程序通过一个堆栈交换数据: lua_State ///定义static 只能在源文件内部使用 static lua_State* GetLua() { lua_State* lu = luaL_newstate(); /*创建Lua对象*/ luaL_openlibs(lu); // 打开所有 共享库函数 到lua 对象 return lu ; } int _tmain(int argc,char*argv[] ) { lua_State *lua=GetLua() ; luaL_dofile(lua,"./main1.lua"); return 0 ; }
Lua脚本代码
--定义一个table类型 table1={} --Lua的table 可以使数组 可以使map 可以使混合类型 这一点和PHP的 array(xx=>xx,1,2)很类似 --Lua的table 可以存放任何数据 包括函数 --此处创建的是索引表 for i=1,10,1 do table1[i]=i; end ---下面是几种遍历表的方式 ---key value迭代方式 print('--------------------------基于索引数组的检索------------------------------'); print("key value 方式迭代集合!") for key,value in pairs(table1) do print(key,":",value) end ---根据索引方式 print("索引方式取值!") for i=1,#table1,1 do print(i,":",table1[i]) end print("aaaaaaa") --[[ for i=1,table.maxn(table1) do print(i,":",table1[i]) end ]]-- --根据ipairs方式取 print("i key value pairs方法") for k,v in ipairs(table1) do print(k,":",v) end --table成员拼接成字符串 print(table.concat(table1,'#')) print(table.concat(table1,'#',1,5)) print('--------------------------基于索引数组的检索------------------------------'); print("key value 方式迭代集合!") table1={} --清空表 --创建混合table table1= { {['name']='张三',['age']=15,['sex']="male",2}, {['name']='李四',['age']=15,['sex']="male"}, {name='王五',['age']=18,['sex']="femal"}, 5, test=1 } ---根据索引方式 print("索引方式取值!") for i=1,#table1,1 do if ( type(table1[i]) == "table") then print("Index:",i,":",table1[i]['name'],table1[i]['age'],table1[i]['sex']) else print("Index:",i,":",table1[i],'is not a table!') end end ---根绝key value方式取值 print("根据key value方式取值") print(table.concat(table1[2],"#")) --内部闭包通过 foreach提供函数 来实现 table.foreach(table1, function(k,v) if ( type(v) == "table") then print("Index:",k,":",v['name'],v['age'],v['sex']) else print("Index:",k,":",v,'is not a table!') end end ) --通过foreachi进行 IndexedBase 迭代 print('foreachi只能迭代基于索引的table') table.foreachi(table1, function(k,v) print('Index:',k,':',v) end ) print('通过for ipairs迭代 只能迭代Indexed Base') for k,v in ipairs(table1) do print("Index:",k,":",v) end print('通过for pairs 迭代 All') for k,v in pairs(table1) do print("Index:",k,":",v) end ---返回table中的元素个数 ---只能获取Indexed Base元素的个数 elementCount=table.getn(table1) print('element count is',elementCount) ---设置table中元素的个数 ---table.setn(table1,2) 已经被废弃 --print('通过for pairs 迭代 All') --for k,v in pairs(table1) do -- print("Index:",k,":",v) --end --clear table ----定义一个输出table的函数 function ShowTable(tb) for i=1,#tb,1 do if ( type(tb[i]) == "table") then print("Index:",i,":",tb[i]['name'],table1[i]['age'],tb[i]['sex']) else print("Index:",i,":",tb[i],'is not a table!') end end end function ShowTableAll(tb) for k,v in pairs(tb) do if ( type(v) == "table") then print("Index:",k,":",v['name'],v['age'],v['sex']) else print("Index:",k,":",v,'is not a table!') end end end ---table的插入 function insertElementFunc() print('this is a func element') end --插入函数元素 table.insert(table1,1,insertElementFunc) ShowTable(table1) --调用插入的函数 table1[1]() --直接通过扩展添加函数成员 动态类型 table1.func=insertElementFunc ; table1.func(); ----显示所有的table元素 ----包括关联元素 以及 索引元素 ShowTableAll(table1) --从table中移除元素 --如果参数Pios没有设置那么默认就是从 尾部删除 --remove只会删除基于索引的 如果要删除关联元素 那么只需设置 nil print("删除元素测试:") table.remove(table1,1) table.remove(table1,1) table.remove(table1,1) table.remove(table1); --删除关联元素 table1.test=nil ----包括关联元素 以及 索引元素 ShowTableAll(table1) --退出脚本
最后界面
相关文章推荐
- lua 与 c/c++ 交互(3)c/c++ 调用 lua 数组类型 table
- 一个使用c++在lua中创建自定义数据类型的简易方法
- Qt实例分析——C++自定义类型的属性在QML的使用
- (使用lua++)Lua脚本和C++交互(三)
- 关于C++ map容器的使用和pair类型及函数
- Lua和C++交互 学习记录之四:全局table交互
- 关于C++ code coverage tool 的研究(3)—gcov使用实例
- [lua] 使用lua string作为二进制buffer和c/c++交互
- lua 与 c/c++ 交互(6) lua调用C++(使用数组 和字符串函数)
- lua 中 lua_gettable的使用 C++ 调用lua 读取table
- Lua中使用table.concat连接大量字符串实例
- 在C++使用LUA交互,LUA实现闭包,C++/LUA相互闭包
- (使用lua++)Lua脚本和C++交互(四)
- Luajit笔记---关于如何FFI与C++代码交互
- Lua Table类型的使用
- 使用cocos2d-x 3.x lua 开发时 lua与c++交互
- 关于lua的module函数 传参类型变成table
- Lua和C++交互 学习记录之九:在Lua中以面向对象的方式使用C++注册的类
- lua知识点1-使用lua string作为二进制buffer和c和c++交互
- 关于C++中String类型的使用