如何在C ++中查找二维数组大小 [英] how to find 2d array size in c++

查看:167
本文介绍了如何在C ++中查找二维数组大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C ++中找到2D数组的大小?是否有像sizeof这样的预定义函数来确定数组的大小?

How do I find the size of a 2D array in C++? Is there any predefined function like sizeof to determine the size of the array?

还有,有人可以告诉我如何在尝试获取未设置的值的同时在数组getvalue方法中检测错误吗?

Also, can anyone tell me how to detect an error in the getvalue method for arrays while trying to get a value which is not set?

推荐答案

假设只允许使用数组,则可以通过以下方式找到二维数组的大小.

Suppose you were only allowed to use array then you could find the size of 2-d array by the following way.

  int ary[][5] = { {1, 2, 3, 4, 5},
                   {6, 7, 8, 9, 0}
                 };

  int rows =  sizeof ary / sizeof ary[0]; // 2 rows  

  int cols = sizeof ary[0] / sizeof(int); // 5 cols

这篇关于如何在C ++中查找二维数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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