C++对Lua中table进行读取、修改和创建
C++代码:
// LuaAndC.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string.h>
using namespace std;
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
int _tmain(int argc, _TCHAR* argv[])
{
//1.创建一个state
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L,"Hello.lua");
//获取table的值
lua_getglobal(L,"str");
//会将get的对应项压到栈顶
lua_getfield(L,1,"name");
lua_getfield(L,1,"age");
if(lua_isstring(L,2))
{
cout<<"name= "<<lua_tostring(L,-2)<<endl;
}
if(lua_isnumber(L,3))
{
cout<<"age= "<<lua_tonumber(L,-1)<<endl;
}
lua_pop(L,2);
//修改table的项
lua_pushfstring(L,"So so");//改完弹出
lua_setfield(L,1,"name");
lua_getfield(L,1,"name");
if(lua_isstring(L,2))
{
cout<<"name1= "<<lua_tostring(L,2)<<endl;
}
lua_pop(L,2);
//新建table
lua_newtable(L); //压栈
lua_pushnumber(L,11);
lua_pushstring(L,"New");
lua_setfield(L,1,"name");
lua_setfield(L,1,"age");
lua_getfield(L,1,"name");
lua_getfield(L,1,"age");
if(lua_isstring(L,2))
{
cout<<"name= "<<lua_tostring(L,2)<<endl;
}
if(lua_isnumber(L,3))
{
cout<<"age= "<<lua_tonumber(L,3)<<endl;
}
lua_pop(L,3);
//关闭state
lua_close(L);
int i;
cin>>i;
return 0 ;
}
Lua代码:
str={name="Hunter",age=18}
C++对Lua中table进行读取、修改和创建的更多相关文章
- lua中 table 元表中元方法的重构实现
转载请标明出处http://www.cnblogs.com/zblade/ lua作为游戏的热更新首选的脚本,其优势不再过多的赘述.今天,我主要写一下如何重写lua中的元方法,通过自己的重写来实现对l ...
- lua中 table 重构index/pairs元方法优化table内存占用
转载请标明出处http://www.cnblogs.com/zblade/ lua作为游戏的热更新首选的脚本,其优势不再过多的赘述.今天,我主要写一下如何重写lua中的元方法,通过自己的重写来实现对l ...
- lua中 table.getn(t) 、#t、 table.maxn(t) 这三个什么区别?
lua中 table.getn(t) .#t. table.maxn(t) 这三个什么区别? RTlocal t = {1,888,x= 999,b=2,5,nil,6,7,[10]=1,8,{z = ...
- Lua中Table的学习
--table 是 Lua 的一种数据结构,用来帮助我们创建不同的数据类型,如:数组.字典等 --Lua也是通过table来解决模块(module).包(package)和对象(Object)的. 例 ...
- Lua中table的实现-《Lua设计与实现》
本文来自<Lua设计与实现>的阅读笔记,推荐Lua学习者可以购买一本,深入浅出讲解lua的设计和实现原理,很赞,哈哈 Lua中对于表的设计,是基于数组和散列表,和其他语言不同,对于数组 ...
- lua中table的遍历,以及删除
Lua 内table遍历 在lua中有4种方式遍历一个table,当然,从本质上来说其实都一样,只是形式不同,这四种方式分别是: 1. ipairs for index, value in ipair ...
- lua中table如何安全移除元素
在Lua中,table如何安全的移除元素这点挺重要,因为如果不小心,会没有正确的移除,造成内存泄漏. 引子 比如有些朋友常常这么做,大家看有啥问题 将test表中的偶数移除掉local test = ...
- lua中table的常用方法
转载:https://blog.csdn.net/Fenglele_Fans/article/details/83627021 1:table.sort() language = {"lua ...
- element-ui中 table表格hover 修改背景色
增加样式级别就行啦 .el-table--enable-row-hover .el-table__body tr:hover>td{ background-color: #212e3e !i ...
随机推荐
- spring4x,暂时停更
spring4x,暂时停更 鄙人愚笨,没有spring基础,直接上了spring4x,发现无法理解(另外spring4x实战课本演示不详,本人学识有限),现从spring3开始.
- android开发环境:使用Android Studio搭建Android集成开发环境(图文教程)
开发环境情况: 物理机版本:Win 7旗舰版(64位) Java SDK版本:jdk1.8.0_25(64位) Android SDK版本:Android 7.1(API 25) Android St ...
- 如何退出telnet
ctrl键+ENter键 然后输入 进入telnet 命令 quit
- 003——VUE操作元素属性
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 017对象——对象 get_object_vars get_parent_class is_subclass_of interface_exists
<?php /** */ //get_object_vars($obj) 获得对象的属性,以关联数据形式返回. /*class study{ public $name; public $age; ...
- C++轮子队-第三周(需求改进&原型设计)
需求改进&原型设计 一.需求完善 (一)系统功能(补充) 图形界面(图片如下图所示:) 根据需求与组内讨论结果,现归纳图形界面方面需要的设计与相应功能: 数据-图形界面中间类: 数字方块类 N ...
- //可以不保存在session中, 并且前面我保存在request,这里session也可以获取 chain.doFilter(request, response); //只有登录名不为空时放行,防止直接登录 成功的页面
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOE ...
- 关于this指向问题的总结【转自秘密花园】
this 的工作原理 JavaScript 有一套完全不同于其它语言的对 this 的处理机制. 在五种不同的情况下 ,this 指向的各不相同. 第一种:全局范围内 this; 当在全部范围内使用 ...
- C# 操作嵌入的资源
被嵌入的程序执行文件 namespace AppTest { class Program { static void Main(string[] args) { Console.WriteLine(& ...
- jquery移除、绑定、触发元素事件
unbind(type [,data]) //data是要移除的函数 $('#btn').unbind("click"); //移除click $('#btn').unbind() ...