gpt4 book ai didi

c++ - lua函数到C++函数

转载 作者:行者123 更新时间:2023-11-30 05:30:35 25 4
gpt4 key购买 nike

我正在努力将 lua 程序转换为 C++ 程序,但我遇到了障碍,我不知道如何将其转换为 C++

function newPool()
local pool = {}
pool.species = {} --imports data from local species = {}
pool.generation = 0
pool.innovation = Outputs
pool.currentSpecies = 1
pool.currentGenome = 1
pool.currentFrame = 0
pool.maxFitness = 0

return pool
end

我知道这两种语言的许多基础知识,我知道它在 lua 中有效,但我需要它在 C++ 中。谁能帮帮我?

最佳答案

Lua 有一个叫做 Tables 的东西它允许您在没有预定义 struct 的情况下添加键值对就像在 C/C++ 中一样。因此,您发布的 Lua 代码是将键值对添加到 pool (阅读代码中的注释):

local pool = {}           -- Declare a new Table
pool.species = {} -- Add another Table to pool called 'species'
pool.generation = 0 -- Add the key 'generation' with value '0'
pool.innovation = Outputs -- Add the key 'innovation' with value 'Outputs'
pool.currentSpecies = 1 -- Add the key 'currentSpecies' with value '1'
pool.currentGenome = 1 -- Add the key 'currentGenome' with value '1'
pool.currentFrame = 0 -- Add the key 'currentFrame' with value '0'
pool.maxFitness = 0 -- Add the key 'maxFitness' with value '0'

在 C++ 中,您有多种选择。 1)你可以创建一个struct并声明你需要什么(我猜测某些数据类型,但如果你有完整的 Lua 程序,你可以弄清楚它们):

struct Pool
{
Species species; // You'll have to define Species in another struct
int generation;
SomeEnum innovation; // You'll have to define SomeEnum in an enum
int currentSpecies;
int currentGenome;
int currentFrame;
int maxFitness;
}

如果你有一个类,那么你可以使用 struct Pool如下所示(将上面的 struct Pool 定义添加到 class Kingdom 上方的 .h 文件中):

// I'm doing this as a class since you are programming in C++ and I
// assume you will want to add more functions to operate on similar
// objects.
class Kingdom
{
public:
Kingdom();
Pool* NewPool();
private:
Pool _pool;
}

在您的 .cpp 文件中:

#include "Kingdom.h"

Kingdom::Kingdom()
{
// _pool.species = whatever you define struct Species as
_pool.generation = 0;
_pool.innovation = SomeEnum::Outputs; // You'll have to define SomeEnum
_pool.currentSpecies = 1;
_pool.currentGenome = 1;
_pool.currentFrame = 0;
_pool.maxFitness = 0;
}

Pool* Kingdom::NewPool()
{
Pool* newPool = new Pool;
memcpy(newPool, &_pool, sizeof(Pool)); // Make a copy
return newPool; // Return the new copy

// The newPool value is dynamic memory so when the calling function is done
// with newPool it should delete it, example:
// Kingdom myKingdom;
// Pool* myNewPoolStruct = myKingdom.NewPool();
// ... do some coding here
// delete myNewPoolStruct;
}

选项 2) 如果您所有的键值对都是同一类型;即所有键都是 std::string所有值都是 int .请记住,Lua 代码使用表,因此您可以在 C++ 中使用 std::map<> 创建等效项.然后你可以使用 std::map<std::string, int>如下:

// In your .h file change
Pool* NewPool();
Pool _pool;
// to
std::map<std::string, int> NewPool();
std::map<std::string, int> _pool;

然后在您的 .cpp 文件中将构造函数更改为:

Kingdom::Kingdom()
{
_pool["species"] = 0; // Some int representation of species
_pool["generation"] = 0;
_pool["innovation"] = 1; // Some int representation of Outputs
_pool["currentSpecies"] = 1;
_pool["currentGenome"] = 1;
_pool["currentFrame"] = 0;
_pool["maxFitness"] = 0;
}

std::map<std::string, int> NewPool()
{
std::map<std::string, int> newPool;
newPool = _pool; // Copy - double check this against std::map
return newPool; // Double check this is a true copy and not a pointer
}

std::map您可以像您提供的 Lua 代码一样即时创建键值对。简而言之,我会选择 struct Pool方法是因为 std::map<>你必须记住字符串,这不是好的做法,你的 IDE 应该有智能感知,它将始终向你显示 struct Pool 的内容。每当你点击 .->运营商。

关于c++ - lua函数到C++函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35933163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com