我有一个问题,让我们说:找到所有两对数字(x,y)和(z,t),使x³+y³=z³+t³,其中(x,y)!=(z,t)和x³ +y³<10,000.
取10,000 10,000的立方根21.544 - >向下舍入到21,所以我得到:
#include <iostream>
using namespace std;
int main() {
for( int x = 1; x <= 20; ++x ) {
for( int y = x + 1; y <= 21; ++y ) {
for( int z = x + 1; z <= y - 1; ++z ) {
for( int t = z; t <= y - 1; ++t ) {
if( x*x*x + y*y*y …Run Code Online (Sandbox Code Playgroud) 我正在尝试让Lua使用新的编程语言D.一切正常(库,lua52.dll等)但luaL_getmetatable崩溃了.最初,函数没有在dlua中定义,但我添加了它:
//C #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
void luaL_getmetatable(lua_State* L, const(char)* s) {
lua_getfield(L, LUA_REGISTRYINDEX, s);
}
Run Code Online (Sandbox Code Playgroud)
但是当我跑步时:
L = lua_open();
luaL_openlibs(L);
// prevent script kiddies
luaL_dostring(L, "os = nil; io = nil");
// reprogram 'print'
luaL_newmetatable(L, "vector");
luaL_getmetatable(L, "vector"); // CRASH
Run Code Online (Sandbox Code Playgroud)
它崩溃了.任何想法为什么会这样?
Lua是一种轻松而强大的语言,但有时感觉缺少一些我们在其他语言中习惯的非常方便的功能.我的问题是关于嵌套if条件.在Perl,Python,C++中,我通常倾向于避免嵌套构造并尽可能编写普通代码,如:
# Perl:
for (my $i = 0; $i < 10; ++$i) {
next unless some_condition_1();
next unless some_condition_2();
next unless some_condition_3();
....
the_core_logic_goes_here();
}
Run Code Online (Sandbox Code Playgroud)
Lua缺少该语句next或continue语句,因此相同的代码将如下所示:
-- Lua:
for i = 1, 5 do
if some_condition_1() then
if some_condition_2() then
if some_condition_3() then
the_core_logic_goes_here()
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
所以我想知道是否有标准方法来避免ifLua中的嵌套块?
我有点惊讶,因为我之前读过,该__gc元方法仅针对用户数据调用,而从不针对表调用。(LuaFAQ:为什么 __gc 和 __len 元方法不能在表上工作?)
但是最近我尝试了一下,发现确实有效!在 Lua 5.2.1 上尝试以下代码:
do
local b = setmetatable({a = 1}, {__gc = function(self) print(self.a); end});
end
collectgarbage();
Run Code Online (Sandbox Code Playgroud)
但我在任何地方都找不到这方面的变更日志,所以我有点沮丧并且不敢使用它。
也许,有人可以证明我的建议?或者这是一种无证行为?对于我来说,有一个常规的方法来创建表析构函数会很好,如果我的观察是正确的,我会很高兴。
我遵循了这个样板示例:http : //www.matrix44.net/blog/?p=456
我能够将字符串从 C 发送到 Lua,但是当我传递一个结构体时出现错误(我使用了 lua_newuserdata)。
//s = (ST*) malloc ( sizeof(ST) );
// lua_pushlightuserdata(state, s);
s = lua_newuserdata(state, sizeof(ST));
s->a=11;
s->b=12;
printf ( "s = %p \n", s ); // Prints 0x80a708
result = lua_pcall(state, 0, LUA_MULTRET, 0);
Run Code Online (Sandbox Code Playgroud)
print(foo) --> Prints 0x80a708
io.write("received has:\n", foo.a); --> ERROR : Failed to run script: script.lua:1: attempt to index global 'foo' (a userdata value)
Run Code Online (Sandbox Code Playgroud)
如何将缓冲区转换为结构/表记录?
最终,我的 C 程序不知道结构,它只会将缓冲区传递给 Lua,而 Lua 知道该结构并且需要在那里进行转换。
谢谢, PS:脚本和 C 程序的 printf …
一般来说,使用函数模板会使编译时间显着延长。
一位朋友建议我检查模块(C++20)进行优化。
我认为这根本不会影响编译速度。
我不知道如何测试这个,所以我在这里问。
下面的代码会以某种方式神奇地优化构建过程吗?
定义仍然需要创建和编译,所以不会有任何区别?
数学.xxx:
module;
#include <typeinfo>
export module math;
import <iostream>;
export
template<typename T>
T square(T x) {
std::cout << typeid(T).name() << std::endl;
return x * x;
}
Run Code Online (Sandbox Code Playgroud)
主程序
import math;
void main() {
square(int());
square(double());
}
Run Code Online (Sandbox Code Playgroud) 现在我想传递一个void*指向Lua 的指针,使用userdata?这该怎么做?
顺便说一下,我用过luabind,但是它无法通过一个void*指向Lua堆栈的指针,这很烦人!你们能帮助我吗?
struct Event
{
A* a;
B* b;
...
};
Event *e;
void* instance = (void*)e;
// param is a parameter that is passed from Lua script. param is a Event object. And I cast it into a void* type
string Answer(void* param)
{
WorkEvent *pWorkEvent = static_cast<WorkEvent*>(param);
ASSERT_RET(pWorkEvent, NULL);
string call_id = pWorkEvent->GetCallId();
CCmThreadManager::TType thrd_id = pWorkEvent->GetHandleThrdID();
Coroutine *pco = pWorkEvent->m_pco;
Run Code Online (Sandbox Code Playgroud) 如果我喜欢这个,我会收到错误.我该怎么办?
local function one()
local function two()
local function three()
callMe() -- got error here
end
end
end
local function callMe()
print ("can't call :(")
end
callMe()
Run Code Online (Sandbox Code Playgroud) 我正在从一本书中学习Lua,这本书有点陈旧.我试过在网上搜索,但由于#在我的搜索中使用了这个标志,我得到了令人困惑的结果.
它说,为了使用upvalue你需要使用%标志.
但是当我在我的代码中写它时,我得到一个错误.
Account.new = function (starting_balance)
local self = {}
local balance = starting_balance
self.withdraw = function (v)
%balance = %balance - v;
end
return self
end
Run Code Online (Sandbox Code Playgroud)
错误是: unexpected symbol near '%'
是否有一种新的方法来处理Lua 5.x中的upvalues?
我试图使用:require("socket"),但我在关于魔兽世界的论坛上说我不能使用require命令.有没有其他方法可以实现这一目标?我想将数据从Lua线程传递到Java线程.