C++ 用 0 填充多维数组元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3586774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fill multidimensional array elements with 0's
提问by user54871
I have a 2d and i want to set the elements to zero without looping all the elements
我有一个 2d,我想将元素设置为零而不循环所有元素
int a[100][200];
I can't initialize them at point of declaration.
我无法在声明时初始化它们。
回答by bta
Try
尝试
int a[100][200] = {{0}};
If you initialize some (but not all) of the elements, the rest will be initialized to zero. Here, you are only explicitly initializing the first element of the first sub-array and the compiler is taking care of the rest.
如果您初始化一些(但不是全部)元素,其余的将被初始化为零。在这里,您只是显式初始化第一个子数组的第一个元素,编译器负责其余部分。
回答by Alexander Gessler
Try memset(a,0,sizeof(a));
尝试 memset(a,0,sizeof(a));
This simply overwrites the memory used by the array with 0 bytes. Don't do this for user-defined data types unless you really know what you do. There's also std::fill
and std::fill_n
, which is more C++ish (but not the easiest way here).
这只是用 0 字节覆盖数组使用的内存。除非您真的知道自己在做什么,否则不要对用户定义的数据类型执行此操作。还有std::fill
and std::fill_n
,它更符合 C++ 风格(但不是这里最简单的方法)。
回答by Johannes Schaub - litb
C++ allows multidimensional arrays to be iterated entirely through by a base-element pointer. So you can use std::fill
and pass it the very first nonarray element
C++ 允许通过基本元素指针完全迭代多维数组。所以你可以使用std::fill
并传递第一个非数组元素
std::fill(a[0] + 0, a[99] + 100, 0);
In C this is formally not allowed, and you would need to iterate through each sub-array separately, because iterating beyond the first subarray's past-the-end pointer causes undefined behavior in C. Nontheless, in practice this still works.
在 C 中,这在形式上是不允许的,您需要分别迭代每个子数组,因为迭代超出第一个子数组的后尾指针会导致 C 中的未定义行为。尽管如此,实际上这仍然有效。
回答by rcanepa
For C++, you can use the std:fill method from the algorithm header.
对于 C++,您可以使用算法头文件中的 std:fill 方法。
int a[x][y];
std::fill(a[0], a[0] + x * y, 0);
So, in your case, you could use this:
所以,在你的情况下,你可以使用这个:
int a[100][200];
std::fill(a[0], a[0] + 100 * 200, 0);
Of course, the 0 could be changed for any int value.
当然,可以为任何 int 值更改 0。
回答by AnT
What exactly does "I can't initialize them at point of declaration" mean? "Don't know how to"? Or "can't modify the code there"?
“我无法在声明时初始化它们”究竟是什么意思?“不知道怎么做”?或者“不能修改那里的代码”?
The C++ language has a feature that initializes the entire array to 0 at the point of declaration
C++ 语言有一个特性,就是在声明点将整个数组初始化为 0
int a[100][100] = {};
(note, no explicit 0
is really necessary between the {}
). Can you use it or not?
(注意,0
在 之间没有明确的必要{}
)。你能不能用?
If you can't, then your options are: use the memset
-hack, 1D-reinterpretation-hack or set each element explicitly by iterating through the array(s) (with or without the help fo std::fill
).
如果你不能,那么你的选择是:使用memset
-hack、1D-reinterpretation-hack 或通过遍历数组(有或没有帮助 fo std::fill
)来显式设置每个元素。
回答by Will A
memset(a, 0, 100 * 200 * sizeof(int));
ought to do it.
memset(a, 0, 100 * 200 * sizeof(int));
应该这样做。
回答by zwol
If this array is declared at file scope, or at function scope but with 'static', then it is automatically initialized to zero for you, you don't have to do anything. This is only good for oneinitialization, at program startup; if you need to reset it you have to code that yourself. I would use memset
for that.
如果这个数组是在文件范围内声明的,或者在函数范围内声明的但带有“静态”,那么它会自动为您初始化为零,您无需执行任何操作。这仅适用于程序启动时的一次初始化;如果你需要重置它,你必须自己编码。我会用memset
的。
If it's declared at function scope without static, you need to use memset
, or an explicit initializer - a single = { 0 }
is enough, you don't need to write out all 2002zeroes like someone else suggested.
如果它在没有静态的函数范围内声明,则需要使用memset
,或显式初始化程序 - 一个= { 0 }
就足够了,您不需要像其他人建议的那样写出所有 200 2 个零。
回答by Mark B
The memset
approach mentioned (memset(a,0,sizeof(a));
) will work, but what about doing it the C++ way (per your tag), and using vector?
memset
提到的方法 ( memset(a,0,sizeof(a));
) 会起作用,但是如何使用 C++ 方式(根据您的标签)并使用 vector 呢?
std::vector<std::vector<int> > a;
a.assign(100, std::vector<int>(200));
回答by gmlacrosse
I tested this solution and it worked.
我测试了这个解决方案,它奏效了。
int arr[100][200];
for (int i=0; i<100; i++)
for (int j=0; j<200; j++) (arr[i][j] = 0);
for (int i=0; i<100; i++)
for (int j=0; j<200; j++) cout << (arr[i][j]);
回答by Vilius
Many answers used pointer arithmetic with fill
. This can be done simpler:
许多答案使用指针算术和fill
. 这可以更简单地完成:
int a[N][K];
fill(a[0], a[N], 0);
Basically, a[N]
is a first memory address after the multi-dimensional array, no matter how many dimensions there are. This works too:
基本上a[N]
就是多维数组之后的第一个内存地址,不管有多少维。这也有效:
int a[N][K][L][M];
fill(**a[0], **a[N], 0);
The asterisks here dereferences the pointers down to int*
type (the array brackets dereferences int****
to int***
, the two asterisks does the rest of the job).
这里的星号将指针解引用到int*
类型(数组括号解引用int****
到int***
,两个星号完成剩下的工作)。