实例 C++ 获取数组的长度/大小

x
 
#include <iostream>
#include <string>
using namespace std;
int main() {
  string cars[] = {"Volvo", "BMW", "Ford", "Mazda"};
  int getArraySize = sizeof(cars) / sizeof(cars[0]);
  cout << "Size of array is: " << getArraySize;
  return 0;
}
                    

输出结果

Size of array is: 4