国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C++遍歷Lua table的方法實例

2024-07-21 23:04:41
字體:
來源:轉載
供稿:網友

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:小麗
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河池市| 迁西县| 贵港市| 西安市| 常熟市| 车致| 扬州市| 安图县| 宁蒗| 拉孜县| 阳山县| 红原县| 海南省| 达日县| 阿图什市| 和平县| 黄平县| 安化县| 武山县| 鸡泽县| 比如县| 信宜市| 酒泉市| 舒城县| 昭苏县| 南陵县| 年辖:市辖区| 泰宁县| 广宁县| 邓州市| 冷水江市| 乌什县| 高清| 同德县| 宣汉县| 阳朔县| 伊金霍洛旗| 什邡市| 新闻| 南康市| 宕昌县|