Share length of array c++

redmeercat484

New member
## Độ dài của mảng trong C ++

Độ dài của một mảng là số lượng các yếu tố mà nó chứa.Trong C ++, bạn có thể tìm thấy độ dài của một mảng bằng phương thức `kích thước ()`.Phương thức `kích thước ()` Trả về số lượng các phần tử trong mảng, dưới dạng số nguyên.

Ví dụ: đoạn mã sau tìm thấy độ dài của một mảng số nguyên:

`` `C ++
int mảng [] = {1, 2, 3, 4, 5};
int length = sizeof (mảng) / sizeof (mảng [0]);
`` `

Toán tử `sizeof ()` trả về kích thước của một mảng trong byte.Biểu thức `sizeof (mảng [0])` trả về kích thước của một phần tử trong mảng.Bằng cách chia kích thước của mảng cho kích thước của một phần tử, chúng ta có thể nhận được số lượng phần tử trong mảng.

Bạn cũng có thể sử dụng hàm `std :: size ()` để có độ dài của một mảng.Chức năng `std :: size ()` là thành viên của lớp `std :: vector`.Để sử dụng chức năng `std :: size ()`, trước tiên bạn phải bao gồm tệp tiêu đề `<vector>`.

Ví dụ: đoạn mã sau đây tìm thấy độ dài của một mảng các chuỗi bằng cách sử dụng hàm `std :: size ()`:

`` `C ++
#include <Vector>

std :: vector <std :: String> arr = {"xin chào", "thế giới"};
int length = mảng.size ();
`` `

Hàm `std :: ()` Trả về số lượng phần tử trong vectơ.Trong trường hợp này, vectơ chứa hai phần tử, vì vậy hàm `std :: ()` trả về 2.

### hashtags

* #C ++
* #Mảng
* #chiều dài
* #kích cỡ
* #vector
=======================================
## Length of Array in C++

The length of an array is the number of elements it contains. In C++, you can find the length of an array using the `size()` method. The `size()` method returns the number of elements in the array, as an integer.

For example, the following code snippet finds the length of an array of integers:

```c++
int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr) / sizeof(arr[0]);
```

The `sizeof()` operator returns the size of an array in bytes. The `sizeof(arr[0])` expression returns the size of one element in the array. By dividing the size of the array by the size of one element, we can get the number of elements in the array.

You can also use the `std::size()` function to get the length of an array. The `std::size()` function is a member of the `std::vector` class. To use the `std::size()` function, you must first include the `<vector>` header file.

For example, the following code snippet finds the length of an array of strings using the `std::size()` function:

```c++
#include <vector>

std::vector<std::string> arr = {"Hello", "World"};
int length = arr.size();
```

The `std::size()` function returns the number of elements in the vector. In this case, the vector contains two elements, so the `std::size()` function returns 2.

### Hashtags

* #C++
* #Array
* #length
* #Size
* #vector
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top