c++ - 如何查找lua堆栈中有多少项目(值)
问题描述
我正在使用 C++ 中的 lua,我想找出在 lua 堆栈中使用了多少“插槽”(你可以说),如果可能的话,lua 堆栈的大小是多少?
解决方案
lua_gettop(lua_State* L)
堆栈中的元素个数与顶部插槽的索引相同。如果您对此感兴趣,可以使用此信息为您打印整个堆栈。
int top = lua_gettop(L);
std::string str = "From top to bottom, the lua stack is \n";
for (unsigned index = top; index > 0; index--)
{
int type = lua_type(L, index);
switch (type)
{
// booleans
case LUA_TBOOLEAN:
str = str + (lua_toboolean(L, index) ? "true" : "false") + "\n";
break;
// numbers
case LUA_TNUMBER:
str = str + std::to_string(lua_tonumber(L, index)) + "\n";
break;
// strings
case LUA_TSTRING:
str = str + lua_tostring(L, index) + "\n";
break;
// other
default:
str = str + lua_typename(L, type) + "\n";
break;
}
}
str = str + "\n";
std::cout << str;
推荐阅读
- c++ - 通过宏获取一串类变量?
- python - 'DataFrame' 对象没有属性 'series' python 错误
- scala - Spark Scala - 过滤器中的按位运算
- python - 二维数组python打印每一行,方括号仍然附加python
- r - 如何使用 fct_reorder() 仅通过另一个向量的子集对因子进行重新排序?
- c# - 为什么我不能在 C# 中的另一个方法中调用 Main 方法中的变量?
- javascript - 在导入时使用 rest 运算符解构
- jenkins-job-dsl - 詹金斯重新加载dsl
- json - 如何在 .env 文件中定义对象数组?
- python - Matplotlib 在不同形状图之间填充