在C++中没有像java一样提供的计算字符串的方法,所以要自己去定义数组的计算。
方法如下:
#include "stdafx.h"
#include <iostream>
void arrytest();
#define GET_ARR_LEN(__X__) (sizeof(__X__) / sizeof(*(__X__)))
int main(){
using namespace std;
arrytest();
}
//数组
void arrytest(){
using std::endl;
using std::cin;
using std::cout;
//cout << "数组" << endl;
int a[] = { 1 ,22,2,3,4,5};
int ArrLen = GET_ARR_LEN(a);
for (int i = 0; i < ArrLen; i++)
{
cout << "数组" <<a[i]<< endl;
}
}
结果:
说明:
#define GET_ARR_LEN(__X__) (sizeof(__X__) / sizeof(*(__X__)))
这句宏是用来计算数组长度的,数组的地址长度/数组类型。