C++ 是不是;";键在Lua表中有特殊意义吗?
使用以下代码从C++创建Lua表:C++ 是不是;";键在Lua表中有特殊意义吗?,c++,lua,associative-array,C++,Lua,Associative Array,使用以下代码从C++创建Lua表: // set first element "1" to value 45 lua_pushnumber( state, 1 ); lua_pushnumber( state, 45 ); lua_rawset( state, -3 ); // set the number of elements (index to the last array element) lua_pushliteral( state, "n" );
// set first element "1" to value 45
lua_pushnumber( state, 1 );
lua_pushnumber( state, 45 );
lua_rawset( state, -3 );
// set the number of elements (index to the last array element)
lua_pushliteral( state, "n" );
lua_pushnumber( state, 1 );
lua_rawset( state, -3 );
最后一个块似乎意味着Lua表具有某种特殊意义的键“n”,根据该示例,该键存储最后一个数组元素的索引
但我在书中找不到这方面的任何参考
- 我的猜测是对还是错
- 如果它是正确的,有人能给我指一个好的参考来解释这个“n”键吗
- 如果错误,示例中第二块代码的含义是什么
- 看看这个:
n
表示数组的长度。它最常用于getn()
函数,该函数只返回表中元素的数量。过去习惯于保持表的大小。我相信Lua5.1,他们不赞成将其作为支持#
操作符的一种做法,因为有时它似乎会与人们在表格中填充的数据发生神奇的冲突。PiL的在线版本适用于Lua5.0。对于5.2Hmm,基本上是相同的,不。不赞成使用n
table.getn()
和table.setn()
在5.2中甚至不再存在。@catwell-当推到表上时,我需要在5.2中设置它吗?ThxWell,如果您不在其他地方明确使用它,则不会。n
的唯一用途是能够以比#
更快的速度获得表格的长度,但通常您不需要它。当推到表格上时,是否需要在5.2中设置它?Thxno,除非您使用的是需要在特定应用程序中设置的遗留脚本。如果在v5.2中,只需使用#
操作符+