主頁 > 知識(shí)庫 > C++遍歷Lua table的方法實(shí)例

C++遍歷Lua table的方法實(shí)例

熱門標(biāo)簽:清朝地圖標(biāo)注哈爾濱 漳州智云呼電話機(jī)器人 個(gè)人怎么在地圖標(biāo)注需要的店鋪 冀州市地圖標(biāo)注 武漢外呼防封系統(tǒng)多少錢 地圖標(biāo)注大廈 怎么去除地圖標(biāo)注 百度地圖標(biāo)注早餐區(qū)域 新岸線智能電銷機(jī)器人

Lua table數(shù)據(jù)如下:

復(fù)制代碼 代碼如下:

--$ cat test.lua lua文件
user = {
        ["name"] = "zhangsan",
        ["age"] = "22",
        ["friend"] = {
                [1] = {
                    ["name"] = "小麗",
                    ["sex"] = "女",
                    ["age"] = "20",
                },
                [2] = {
                    ["name"] = "小羅",
                    ["sex"] = "男",
                    ["age"] = "20",
                },
            },
        }

要讀出上面table 中所有數(shù)據(jù),C++代碼如下:

復(fù)制代碼 代碼如下:

//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;
}

運(yùn)行結(jié)果:

復(fù)制代碼 代碼如下:

$ ./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++中explicit與mutable關(guān)鍵字的深入探究
  • C++中const、volatile、mutable使用方法小結(jié)
  • C++中mutable與volatile的深入理解
  • 淺談C++中的mutable和volatile關(guān)鍵字
  • C++中MFC Tab Control控件的使用詳解
  • Lua教程(三):C語言、C++中調(diào)用Lua的Table示例
  • c++遍歷lua table示例
  • 深入解析C++中的mutable關(guān)鍵字
  • c++關(guān)鍵字mutable深入解析
  • C++解析特殊符號(hào)tab和換行符號(hào)詳情

標(biāo)簽:宣城 金昌 天門 儋州 德宏 天門 臺(tái)灣 濰坊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《C++遍歷Lua table的方法實(shí)例》,本文關(guān)鍵詞  C++,遍歷,Lua,table,的,方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《C++遍歷Lua table的方法實(shí)例》相關(guān)的同類信息!
  • 本頁收集關(guān)于C++遍歷Lua table的方法實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章