CAPI c++ 遍历lua表

简介:

1.一些用来遍历lua表的api简介

以下是对下面几个函数的认识不对请批评指正:

lua_istable:是否是一个表 
lua_gettable(L,int index) :把lua栈的索引为index表的lua栈的index+1所指的索引的值弹出。也就是弹出table[index+1]; 
lua_next(L,index):先把 表(lua栈 index所指的表), 的当前索引弹出,再把table 当前索引的值弹出,也就是先弹出 table的索引,再弹出table索引的值

2.代码示例

复制代码
// lua_table_extent.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdafx.h"
#include "lua.hpp"
#include "lauxlib.h"
#include "lualib.h"
#include <string.h>
#include <windows.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"lua5.1.lib")
#pragma comment(lib,"lua51.lib")


/*
luaJ_table.lua文件内容
----------------------------------------------
NUMBER_TABLE =
{ 11,
22,
33,
44,
}

NUMBER_TABLE_WITH_INDEX =
{
["a"] = 1,
["b"] = 2,
["c"] = 3
}


STRING_TABLE_WITH_INDEX =
{
["a"] = "this is a",
["b"] = "this is b",
["c"] = "this is c"
}
-----------------------------------------------
*/

int _tmain(int argc, _TCHAR* argv[])
{

    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    if(0 != luaL_loadfile(L,"lua_table.lua"))
    {
        printf("loadbuff error :%s",lua_tostring(L,-1));
        lua_pop(L,1);
    }

    if(0 != lua_pcall(L,0,0,0))
    {
        printf("pcall error :%s",lua_tostring(L,-1));
        lua_pop(L,1);
    }

    lua_getglobal(L,"STRING_TABLE_WITH_INDEX");
    /*此时lua栈状态
    ----------------------------------
    |  -1 table NUMBER_TABLE
    ----------------------------------
    */

    if(!lua_istable(L,-1))
        cout<<"not a table"<<endl;
    /*此时lua栈状态
    ----------------------------------
    |  -1 table NUMBER_TABLE
    ----------------------------------
    */

    lua_pushnumber(L,1);
    /*此时lua栈状态
    ----------------------------------
    |  -1 1 key
    |  -2 table NUMBER_TABLE
    ----------------------------------
    */
    lua_gettable(L,-2);
    /*此时lua栈状态
    ----------------------------------
    |  -1 1 Value
    |  -2 table NUMBER_TABLE
    ----------------------------------
    */
    if(lua_isnumber(L,-1))
        cout<<lua_tonumber(L,-1)<<endl;
    else if(lua_isstring(L,-1))
        cout<<lua_tostring(L,-1)<<endl;
    /*此时lua栈状态
    ----------------------------------
    |  -1 1 Value
    |  -2 table NUMBER_TABLE
    ----------------------------------
    */
    lua_pop(L,1);
    /*此时lua栈状态
    ----------------------------------
    |  -1 table NUMBER_TABLE
    ----------------------------------
    */


    //循环遍历
    lua_pushnil(L);
    /*此时lua栈状态
    ----------------------------------
    |  -1 nil
    |  -2 table NUMBER_TABLE
    ----------------------------------
    */
    while(lua_next(L,-2))
    {
    /*此时lua栈状态
    ----------------------------------
    |  -1 value
    |  -2 key
    |  -3 table NUMBER_TABLE
    ----------------------------------
    */
        if(lua_isnumber(L,-2))
            cout<<"key:"<<lua_tonumber(L,-2)<<'\t';
        else if(lua_isstring(L,-2))
            cout<<"key:"<<lua_tostring(L,-2)<<'\t';
        if(lua_isnumber(L,-1))
            cout<<"value:"<<lua_tonumber(L,-1)<<endl;
        else if(lua_isstring(L,-1))
            cout<<"value:"<<lua_tostring(L,-1)<<endl;

    /*此时lua栈状态
    ----------------------------------
    |  -1 value
    |  -2 key
    |  -3 table NUMBER_TABLE
    ----------------------------------
    */
        lua_pop(L,1);
    /*此时lua栈状态
    ----------------------------------
    |  -1 key
    |  -2 table NUMBER_TABLE
    ----------------------------------
    */
    }
    lua_pop(L,1);

    /*此时lua栈状态
    ----------------------------------
    |  -1 table NUMBER_TABLE
    ----------------------------------
    */
    lua_close(L);
    system("pause");
    return 0;
}
复制代码

以上代码的输出为

key:a   value:this is a
key:c   value:this is c
key:b   value:this is b
请按任意键继续. . .
相关文章
|
3月前
|
C++ 容器
【C++之迭代器】遍历容器
【C++之迭代器】遍历容器
|
3月前
|
C语言 C++
【C++之数组与指针1】随机输入整数存入数组并用指针遍历
【C++之数组与指针1】随机输入整数存入数组并用指针遍历
|
1月前
|
JSON JavaScript 数据格式
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
95 2
|
1月前
|
编解码 算法 程序员
【C++ 泛型编程 高级篇】 C++ 14 模版元编程 遍历元组 编译期生成整数序列 std::index_sequence和std::make_index_sequence 使用指南(三)
【C++ 泛型编程 高级篇】 C++ 14 模版元编程 遍历元组 编译期生成整数序列 std::index_sequence和std::make_index_sequence 使用指南
27 0
|
1月前
|
C++ 索引
【C++ 泛型编程 高级篇】 C++ 14 模版元编程 遍历元组 编译期生成整数序列 std::index_sequence和std::make_index_sequence 使用指南(二)
【C++ 泛型编程 高级篇】 C++ 14 模版元编程 遍历元组 编译期生成整数序列 std::index_sequence和std::make_index_sequence 使用指南
29 0
|
1月前
|
存储 编译器 程序员
【C++ 泛型编程 高级篇】 C++ 14 模版元编程 遍历元组 编译期生成整数序列 std::index_sequence和std::make_index_sequence 使用指南(一)
【C++ 泛型编程 高级篇】 C++ 14 模版元编程 遍历元组 编译期生成整数序列 std::index_sequence和std::make_index_sequence 使用指南
49 0
|
3月前
|
C++ 存储 Serverless
力扣C++|一题多解之数学题专场(2)
力扣C++|一题多解之数学题专场(2)
28 0
力扣C++|一题多解之数学题专场(2)
|
3月前
|
C++
c++ unordered_map4种遍历方式
c++ unordered_map4种遍历方式
38 0
|
3月前
|
算法 C++ 开发者
【C++STL基础入门】排序和遍历容器
【C++STL基础入门】排序和遍历容器
|
4月前
|
存储 前端开发 数据库连接
C++ Qt开发:SqlRelationalTable关联表组件
Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍`SqlRelationalTable`关联表组件的常用方法及灵活运用。在上一篇文章中详细介绍了`SqlTableModle`组件是如何使用的,本篇文章将介绍`SqlRelationalTable`关联表组件,该该组件其实是`SqlTableModle`组件的扩展类,其提供了一个带关系的数据模型,用于处理数据库中的表与表之间的关系。通过这个类,你可以在一个表中使用外键关联到另一个表的数据上。例如将主表中的某
27 0
C++ Qt开发:SqlRelationalTable关联表组件

热门文章

最新文章