C++中遍历读取数组中的元素

答案来源:https://zhidao.baidu.com/question/187071815.html

对于字符数组str[N],判断方法有以下三种:

  • 第一种:用库函数strlen

1
len = strlen(str); // 数组长度
  • 第二种:利用计数器

1
2
int i=0;
while(str[i++] != ''); // 数组str的长度为i
  • 第三种:利用sizeof函数

1
len = sizeof(str)/sizeof(str[0]); // 数组长度

对于其他类型的数组,都可以用字符数组的第三种方法,第三种方法是通用的。

原文地址:https://www.cnblogs.com/jieliujas/p/8806336.html