数组中元素的个数是数组的长度。
解释:java数字中数组的个数可以用length方法获取到。如:
stirng[]
list
=
{12,13,14};
长度就是3,元素的个数也是3.
备注:数组元素下标是从0开始,所以获取值得时候需要注意下。
C++中数组可分为堆区的数组和栈区的数组,对于两种数组C++都没有函数可以直接获取数组的元素的个数。
一、堆区的数组
堆区的数组是自己申请的,比如用new申请空间:
int* arr = new int[10];
堆区的数组不能计算出包含元素个数。
二、栈区的数组
栈区的数组是系统自动分配的,如:
[cpp] view plain copy
int arr[10] = { 1,2,3,4,5,6,7,8,9,0 };
栈区的数组可以通过以下两种方法得出元素的个数:
(1)
[cpp] view plain copy
int arr[10] = { 1,2,3,4,5,6,7,8,9,0 };
auto diff = sizeof(arr)/sizeof(int);
(2)
这种方法需要所用编译器支持C++11,14
[cpp] view plain copy
int arr[10] = { 1,2,3,4,5,6,7,8,9,0 };
int *pbeg = begin(arr);
int *pend = end(arr);
auto length = pend - pbeg;//数组元素个数