c++ - lua数组转换成c++数组

标签 c++ arrays lua

 local pricetagColors = {
    [3242] = {255, 0, 255},
    [6712] = {255, 255, 0}
 }

 function getPricetagColor(itemnumber)
    local r, g, b = 0, 0, 0

    if pricetagColors[itemnumber] then
        r, g, b = pricetagColors[itemnumber][1], pricetagColors[itemnumber][2], pricetagColors[itemnumber[3]
    end

    return {r, g, b}
 end

好的,所以我现在正在尝试逐步进入 C++。 现在我正试图弄清楚 C++ 中的(复杂的?)数组是如何创建的。 因为我不知道如何用另一种方式解释它,所以我在 LUA 中做了它,因为这是我最了解的。 函数并不重要,重要的是数组,因为我已经搜索了几个小时,但我不知道如何在 C++ 中完成你在 lua 中看到的数组。

最佳答案

看起来你在问题中的内容相当于 std::map<int, std::array<int, 3>> .

std::map<int, std::array<int, 3>> pricetagColors;
pricetagColors[3242] = {255, 0, 255};
pricetagColors[6712] = {255, 255, 0};

int itemnumber = 3242, r, g, b;
if (pricetagColors.find(itemnumber) != pricetagColors.end())
{
    r = pricetagColors[itemnumber][0];
    g = pricetagColors[itemnumber][1];
    b = pricetagColors[itemnumber][2]; //Note that the comma operator could be used here, 
                                       //but it isn't really an idiomatic C++ use
}

关于c++ - lua数组转换成c++数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36097232/

相关文章:

android - Exoplayer 从字节数组播放音频 - ByteArrayDataSource

javascript - 将 MongoDB 查询结果转换为 JSONArray

c++ - 如何通过 SWIG 将 lua 嵌入到 C++ 中

c++ - new 后括号的存在,在 C++11 中做任何不同

c++ - Netbeans 在 C++ 中创建了一个默认构造函数。它有什么作用?

c++ - Linux c++ 可移植二进制问题

c++ - 如何注册 Windows 服务但避免它被列在服务控制台中?

Java:如何有选择地将文件扫描到两个二维字符串数组中

c++ - 发生错误后如何确保(luabind)lua 状态良好?

lua - 我可以从通过 string.dump() 检索的 lua 字节码中得到什么?