Home > Backend Development > C#.Net Tutorial > How to calculate array length in c++

How to calculate array length in c++

小老鼠
Release: 2024-05-08 17:48:20
Original
821 people have browsed it

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();

How to calculate array length in c++

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:

  • Use the sizeof() operator:
<code class="cpp">size_t length = sizeof(array) / sizeof(array[0]);</code>
Copy after login
  • Using .size() method (for vector):
<code class="cpp">size_t length = my_vector.size();</code>
Copy after login

Example

<code class="cpp">// 声明一个包含 10 个整数的数组
int my_array[10];

// 计算数组长度
size_t length = sizeof(my_array) / sizeof(int);

// 输出数组长度
cout << "数组长度:" << length << endl;</code>
Copy after login

Output:

<code>数组长度:10</code>
Copy after login

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!

Related labels:
c++
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template