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进行读取、修改和创建的更多相关文章

  1. lua中 table 元表中元方法的重构实现

    转载请标明出处http://www.cnblogs.com/zblade/ lua作为游戏的热更新首选的脚本,其优势不再过多的赘述.今天,我主要写一下如何重写lua中的元方法,通过自己的重写来实现对l ...

  2. lua中 table 重构index/pairs元方法优化table内存占用

    转载请标明出处http://www.cnblogs.com/zblade/ lua作为游戏的热更新首选的脚本,其优势不再过多的赘述.今天,我主要写一下如何重写lua中的元方法,通过自己的重写来实现对l ...

  3. 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 = ...

  4. Lua中Table的学习

    --table 是 Lua 的一种数据结构,用来帮助我们创建不同的数据类型,如:数组.字典等 --Lua也是通过table来解决模块(module).包(package)和对象(Object)的. 例 ...

  5. Lua中table的实现-《Lua设计与实现》

    本文来自<Lua设计与实现>的阅读笔记,推荐Lua学习者可以购买一本,深入浅出讲解lua的设计和实现原理,很赞,哈哈   Lua中对于表的设计,是基于数组和散列表,和其他语言不同,对于数组 ...

  6. lua中table的遍历,以及删除

    Lua 内table遍历 在lua中有4种方式遍历一个table,当然,从本质上来说其实都一样,只是形式不同,这四种方式分别是: 1. ipairs for index, value in ipair ...

  7. lua中table如何安全移除元素

    在Lua中,table如何安全的移除元素这点挺重要,因为如果不小心,会没有正确的移除,造成内存泄漏. 引子 比如有些朋友常常这么做,大家看有啥问题 将test表中的偶数移除掉local test = ...

  8. lua中table的常用方法

    转载:https://blog.csdn.net/Fenglele_Fans/article/details/83627021 1:table.sort() language = {"lua ...

  9. element-ui中 table表格hover 修改背景色

    增加样式级别就行啦   .el-table--enable-row-hover .el-table__body tr:hover>td{ background-color: #212e3e !i ...

随机推荐

  1. shell 数组操作

    1. 定义数组: var_array=(one two three four five) 2.常用操作 获取数组长度: ${#var_array[@]} 获取所有数组元素:  ${var_array[ ...

  2. 如何让pycharm以py.test方式运行

    第一步:进入File—Settings—Python Integrated Tools 发现设置中Default test runner是Unittests 将其改为py.test,点击OK保存 如果 ...

  3. poj3517约瑟夫问题

    直接套公式+ 假设除去第k个人. 0, 1, 2, 3, ..., k-2, k-1, k, ..., n-1 //original sequence (1) 0, 1, 2, 3, ..., k-2 ...

  4. 支付宝VIE的罪与罚

    http://tech.ifeng.com/special/tusimple/alibaba/#_www_dt2   雅虎的杨致远.软银的孙正义,都曾是马云阿里巴巴创业路上的贵人,也都曾是相互信任的朋 ...

  5. html5新增语义化标签

    注意:body.section.nav 需要h1-h6. div.header则不需要. 1):<article> 显示一个独立的文章内容. 例如一篇完整的论坛帖子,一则网站新闻,一篇博客 ...

  6. 【C#】Socket中的ssl通信

    引言    有个项目中用到了Socket ssl通信,在此记录一下. 证书 Socket ssl需要用到证书用来校验身份,而作为调试,我们只需用测试证书即可. 有个工具可以很方便地制作测试证书,下载地 ...

  7. mcake活动维护常见问题记录【pc端】 ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★

    ★ ★ ★ ★ ★ ★ ★ ★ ★ ★pc端问题及解决方法 ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ 问题一.pc.弹窗,背景兼容ie8的写法 ;;; -moz-opacity:.7;f ...

  8. 【python】imp模块的使用

    是import在程序中的使用 [一]函数load_source imp.load_source(moduleName, sourceFile) 使用: abc = imp.load_source('d ...

  9. Mongodb 补充

    1 mongodb 概述 启动mongo 1 数据库操作 没有数据的 集合 和 数据库不会显示 db 查看当前的数据库名称: 所有物理上存在的数据库 db.stats() 查看当前的数据库信息: sh ...

  10. AAC解码算法原理详解