如何在C++函数中将静态数组初始化为特定值?

3

我正在尝试在函数中初始化一个静态数组。

int query(int x, int y) {
    static int res[100][100]; // need to be initialized to -1
    if (res[x][y] == -1) {
        res[x][y] = time_consuming_work(x, y);
    }
    return res[x][y];
}

我该如何实现这个目标?


2
@Hyperbola 你可以使用一个静态布尔变量来保护双重循环执行的初始化... :) 编辑:哦,请检查bolov的答案,它使用λ来初始化数组..! - gsamaras
1
@gsamaras 谢谢,Lambda 真是太神奇了!我从未想过可以这样使用 Lambda。 - hyperbola
1
一个 lambda 表达式也可以用来初始化一个 const 变量:https://dev59.com/Y6Xja4cB1Zd3GeqPP1y0#46345224 - Robert Andrzejuk
1
@bolov,你说得对。我本意只是给一个提示。看来这确实是一道作业题。但如果那是“提示1”,那么“提示2”就是gsamaras的评论,使用静态布尔值作为守卫。那就是我最终要去的地方。 - donjuedo
这并不是我的作业,而是我遇到的问题。 - hyperbola
显示剩余2条评论
4个回答

7

首先,我强烈建议从C数组转移到std :: array。如果您这样做,您可以拥有一个函数来执行初始化(否则您不能,因为函数无法返回C数组):

constexpr std::array<std::array<int, 100>, 100> init_query_array()
{
    std::array<std::array<int, 100>, 100> r{};
    for (auto& line : r)
        for (auto& e : line)
            e = -1;
    return r;
}

int query(int x, int y) {
    static std::array<std::array<int, 100>, 100> res = init_query_array();

    if (res[x][y] == -1) {
        res[x][y] = time_consuming_work(x, y);
    }
    return res[x][y];
}

另一种选择,实际上我更喜欢的是在lambda函数中执行init:

int query(int x, int y) {
    static auto res = [] {
        std::array<std::array<int, 100>, 100> r;
        for (auto& line : r)
            for (auto& e : line)
                e = -1;
        return r;
    }();

    if (res[x][y] == -1) {
        res[x][y] = time_consuming_work(x, y);
    }
    return res[x][y];
}

那种λ的方法非常令人愉悦,而且不会受到额外变量的开销的影响... :) - gsamaras
这太棒了!! - hyperbola
你可以解释一下 static auto res = [] {}() 语法的含义吗? - deon cagadoes
1
@deoncagadoes 这是一个 lambda 表达式 。括号中的 () 调用了我们刚定义的 lambda。 - bolov
1
@deoncagadoes 这是一个立即调用的 lambda 函数 https://en.cppreference.com/w/cpp/language/lambda - Robert Andrzejuk

2

你不能这样做。你需要使用显式的for循环和一个标志来避免多次初始化:

最初的回答

int query(int x, int y) {
    static bool initilized = false;
    static int res[100][100]; // need to be initialized to -1
    if (!initilized) {
        initilized = true;
        for (int i = 0; i != 100; ++i) {
            for (int j = 0; j != 100; ++j) {
                res[i][j] = -1;
            }
        }
    }
    if (res[x][y] == -1) {
        res[x][y] = time_consuming_work(x, y);
    }
    return res[x][y];
}

1
你可以通过引入一个静态变量的方式来实现,例如以下方法。
int query(int x, int y) {
    static bool initialized;
    static int res[100][100]; // need to be initialized to -1

    if ( not initialized )
    {
        for ( auto &row : res )
        {
            for ( auto &item : row ) item = -1;
        }

        initialized = true;
    }        

    if (res[x][y] == -1) {
        res[x][y] = time_consuming_work(x, y);
    }
    return res[x][y];
}

1
你可以使用fillstd::array以及一个IIL(立即调用的lambda表达式):
static std::array<std::array<int, 100>, 100> res = [] () {
     std::array<int, 100> default_arr;
     default_arr.fill(-1);

     std::array<std::array<int, 100>, 100> ret;
     ret.fill(default_arr);

     return ret;
}();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接