我需要用零填充二维数组 [英] I need to fill an 2d array only with zeros

查看:96
本文介绍了我需要用零填充二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用2d数组。一开始我想将整个数组设置为零。我知道对于1d数组我们使用这样的东西:

int arr [100] = {0};

2d的快捷方式是什么?

I'm working with 2d arrays. At the beginning I would like to set my entire array to zero. I know for a 1d array we use something like this:
int arr[100] = {0};
What would be the shortcut for a 2d one??

推荐答案

如果没有指定元素,那么在初始化数组时(在第一次之后)他们初始化为零。所以我们要做的就是指定外部数组的第一个元素:



When you're initialising an array if you don't specify elements (after the first) they're zero initialised. So all we have to do is specify the first element of the outer array:

int arr[10][10] = {{0}};





我们已经完成了。



哦,关于在C ++中使用数组的强制性讲座:你有没有理由使用数组而不是向量向量?创造效率是一个合理的答案,因为创建矢量矢量更昂贵,但是当它们被构建时,它们在大多数情况下都同样有效。



and we're done.

Oh, and mandatory lecture about using arrays in C++: Is there any reason you're using an array rather than a vector of vectors? Efficiency of creation is a legitimate answer as creating vectors of vectors is more expensive but when they're built they're just as efficient in most cases.


这篇关于我需要用零填充二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆