Share c++ pair source code

#C ++ #Pair #Source#tutorial #Programming

## mã nguồn cặp C ++

Một cặp trong C ++ là cấu trúc dữ liệu lưu trữ hai yếu tố cùng loại dữ liệu hoặc khác nhau.Nó được triển khai như một lớp mẫu trong tệp tiêu đề <Utility>.Cú pháp để khai báo một cặp như sau:

`` `C ++
cặp <type1, type2> pair_name;
`` `

Ở đâu,

* `type1` và` type2` là các loại dữ liệu của hai phần tử được lưu trữ trong cặp.
* `Pair_name` là tên của cặp.

Ví dụ: mã sau đây khai báo một cặp số nguyên:

`` `C ++
cặp <int, int> pair_of_integers;
`` `

Để khởi tạo một cặp, bạn có thể sử dụng cú pháp sau:

`` `C ++
cặp <type1, type2> pair_name (value1, value2);
`` `

Ở đâu,

* `value1` và` value2` là các giá trị của hai phần tử được lưu trữ trong cặp.

Ví dụ: mã sau đây khởi tạo một cặp số nguyên với các giá trị 10 và 20:

`` `C ++
cặp <int, int> pair_of_integers (10, 20);
`` `

Bạn có thể truy cập các phần tử của một cặp bằng các hàm thành viên `first` và` second`.Hàm thành viên `đầu tiên` trả về một tham chiếu đến phần tử đầu tiên của cặp và hàm thành viên `thứ hai` trả về một tham chiếu đến phần tử thứ hai của cặp.Ví dụ: mã sau in các giá trị của các phần tử của cặp `Pair_of_Integers`:

`` `C ++
cout << Pair_of_Integers.first << endl;
cout << Pair_of_Integers.second << endl;
`` `

Đầu ra:

`` `
10
20
`` `

Bạn cũng có thể sử dụng hàm thành viên `AT ()` để truy cập các phần tử của một cặp.Hàm `at ()` thành viên lấy chỉ mục số nguyên làm tham số và trả về tham chiếu đến phần tử tại chỉ mục đó.Chỉ mục của phần tử thứ nhất là 0 và chỉ mục của phần tử thứ hai là 1. Ví dụ: mã sau in các giá trị của các phần tử của cặp `pair_of_integers`:

`` `C ++
cout << Pair_of_Integers.at (0) << endl;
cout << Pair_of_Integers.at (1) << endl;
`` `

Đầu ra:

`` `
10
20
`` `

## hashtags

* #C ++
* #Pair
* #mã nguồn
* #tutorial
* #Programming
=======================================
#C++ #Pair #Source Code #tutorial #Programming

## C++ Pair Source Code

A pair in C++ is a data structure that stores two elements of the same or different data types. It is implemented as a template class in the <utility> header file. The syntax to declare a pair is as follows:

```c++
pair<type1, type2> pair_name;
```

where,

* `type1` and `type2` are the data types of the two elements to be stored in the pair.
* `pair_name` is the name of the pair.

For example, the following code declares a pair of integers:

```c++
pair<int, int> pair_of_integers;
```

To initialize a pair, you can use the following syntax:

```c++
pair<type1, type2> pair_name(value1, value2);
```

where,

* `value1` and `value2` are the values of the two elements to be stored in the pair.

For example, the following code initializes a pair of integers with the values 10 and 20:

```c++
pair<int, int> pair_of_integers(10, 20);
```

You can access the elements of a pair using the `first` and `second` member functions. The `first` member function returns a reference to the first element of the pair, and the `second` member function returns a reference to the second element of the pair. For example, the following code prints the values of the elements of the pair `pair_of_integers`:

```c++
cout << pair_of_integers.first << endl;
cout << pair_of_integers.second << endl;
```

Output:

```
10
20
```

You can also use the `at()` member function to access the elements of a pair. The `at()` member function takes an integer index as a parameter and returns a reference to the element at that index. The index of the first element is 0, and the index of the second element is 1. For example, the following code prints the values of the elements of the pair `pair_of_integers`:

```c++
cout << pair_of_integers.at(0) << endl;
cout << pair_of_integers.at(1) << endl;
```

Output:

```
10
20
```

## Hashtags

* #C++
* #Pair
* #Source Code
* #tutorial
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top