圆月山庄资源网 Design By www.vgjia.com

Lua table数据如下:

C++遍历Lua table的方法实例

复制代码 代码如下:
--$ cat test.lua lua文件
user = {
        ["name"] = "zhangsan",
        ["age"] = "22",
        ["friend"] = {
                [1] = {
                    ["name"] = "小丽",
                    ["sex"] = "女",
                    ["age"] = "20",
                },
                [2] = {
                    ["name"] = "小罗",
                    ["sex"] = "男",
                    ["age"] = "20",
                },
            },
        }

要读出上面table 中所有数据,C++代码如下:

复制代码 代码如下:
//C++代码:
#include <lua.hpp>
#include <iostream>
#include <string>
using namespace std;
 
bool popTable(lua_State* L, int idx)
{
    try{
        lua_pushnil(L);
        while(lua_next(L, idx) != 0){
            int keyType = lua_type(L, -2);
            if(keyType == LUA_TNUMBER){
                double value = lua_tonumber(L, -2);
                cout << "Key:" << value << endl;
            }else if(keyType == LUA_TSTRING){
                const char*  value = lua_tostring(L, -2);
                cout << "Key:" << value << endl;
            }else{
                cout << "Invalid key type: " << keyType << endl;
                return false;
            }
            int valueType = lua_type(L, -1);
            switch(valueType){
                case LUA_TNIL:
                {
                    cout << "Value: nil" << endl;
                    break;
                }
                case LUA_TBOOLEAN:
                {
                    int value = lua_toboolean(L, -1);
                    cout << value << endl;
                    break;
                }
                case LUA_TNUMBER:
                {    cout << "Value:" << lua_tonumber(L, -1) << endl;
                    break;
                }
                case LUA_TSTRING:
                {
                    cout << "Value:" << lua_tostring(L, -1) << endl;
                    break;
                }
                case LUA_TTABLE:
                {
 
                    cout << "====sub table===" << endl;
                    int index = lua_gettop(L);
                    if (!popTable(L, index)) {
                        cout << "popTable error in  popTable,error occured" << endl;
                        return false;
                    }
                    break;
                }
                default:
                {
                    cout << "Invalid value type: " << valueType << endl;
                    return false;
                }
            }
            lua_pop(L, 1);
        }
    }catch(const char* s){
       string errMsg = s;
       lua_pop(L,1);
       cout << errMsg << endl;
       return false;
    }catch(std::exception& e){
        const char* errMsg = e.what();
        lua_pop(L,1);
        cout << errMsg << endl;
        return false;
    }catch(...){
        const char* errMsg = lua_tostring(L,-1);
        lua_pop(L,1);
        cout << errMsg << endl;
        return false;
    }
    return true;
}
 
 
int main(int argc, char* argv)
{
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    int r = luaL_dofile(L,"./test.lua");
    lua_getglobal(L, "user");
    int type = lua_type(L,1);
    if(type == LUA_TTABLE){
        int index = lua_gettop(L);
        if(popTable(L,index)){
            return 0;
        }else{
            cout << "Error" << endl;
            return -1;
        }
    }
    return 0;
}

运行结果:
复制代码 代码如下:
$ ./cpptable.linux_64_gcc4
Key:age
Value:22
Key:name
Value:zhangsan
Key:friend
====sub table===
Key:2
====sub table===
Key:sex
Value:男
Key:age
Value:20
Key:name
Value:小罗
Key:1
====sub table===
Key:sex
Value:女
Key:age
Value:20
Key:name
Value:小丽

标签:
C++,遍历,Lua,table

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。