C++如何取得数组元素的个数?

来源:百度知道 编辑:UC知道 时间:2024/07/04 15:18:06
例如 :

int a[100] = {1,2,3,4,5};

sizeof(a) / sizeof(int) 取得的是数组所大小,
我用那个函数知道这个数组有5个元素?
错误: 对成员 ‘length’ 的请求出现在 ‘a’ 中,而后者具有非类类型 ‘int [100]’

没用用到容器,纯数组的个数没有现成的函数可以求。
以下代码可以实现

#include <iostream>
using namespace std;

int main()
{
int a[100] = {1,2,3,4,5};

int i;
for(i=0;a[i];i++);

cout<<i<<endl;

system("pause");
}

a.length