您的位置:首页 > 编程语言 > Lua

C++中遍历lua table

2013-03-20 19:29 190 查看
int lua_next (lua_State *L, int index);

Pops a key from the stack, and pushes a key-value pair from the table at the given index (the "next" pair after the given key). If there are no more elements in the table, then
lua_next
returns 0 (and pushes nothing).

A typical traversal looks like this:

/* table is in the stack at index 't' */
lua_pushnil(L);  /* first key */
while (lua_next(L, t) != 0) {
/* uses 'key' (at index -2) and 'value' (at index -1) */
printf("%s - %s\n",
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}

While traversing a table, do not call
lua_tolstring
directly on a key, unless you know that the key is actually a string. Recall that
lua_tolstring
changes the value at the given index; this confuses the next call to
lua_next
.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: