如何在 C++ 函数中将静态数组初始化为某个值?
How to initialize a static array to certain value in a function in c++?
我正在尝试在函数中初始化静态数组。
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];
}
我怎样才能做到这一点?
你可以通过再引入一个静态变量来实现,例如下面的方式
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];
}
你不能这样做。您需要一个明确的 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];
}
首先,我强烈建议从 C 数组转移到 std::array
。如果你这样做,你可以有一个函数来执行初始化(否则你不能,因为函数不能 return 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 中执行初始化:
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];
}
您可以将 fill
与 std::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;
}();
我正在尝试在函数中初始化静态数组。
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];
}
我怎样才能做到这一点?
你可以通过再引入一个静态变量来实现,例如下面的方式
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];
}
你不能这样做。您需要一个明确的 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];
}
首先,我强烈建议从 C 数组转移到 std::array
。如果你这样做,你可以有一个函数来执行初始化(否则你不能,因为函数不能 return 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 中执行初始化:
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];
}
您可以将 fill
与 std::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;
}();