Method to calculate array length in C: sizeof() operator: size_t length = sizeof(array) / sizeof(array[0]);.size() method (applicable to vector): size_t length = my_vector.size();
C How to calculate the array length
Method to calculate the array length
The length of the array in C can be calculated by the following method:
<code class="cpp">size_t length = sizeof(array) / sizeof(array[0]);</code>
<code class="cpp">size_t length = my_vector.size();</code>
Example
<code class="cpp">// 声明一个包含 10 个整数的数组 int my_array[10]; // 计算数组长度 size_t length = sizeof(my_array) / sizeof(int); // 输出数组长度 cout << "数组长度:" << length << endl;</code>
Output:
<code>数组长度:10</code>
The above is the detailed content of How to calculate array length in c++. For more information, please follow other related articles on the PHP Chinese website!