Javatpoint Logo
Javatpoint Logo

PAIR IN C++

Pair is a term used to combine two values, which may be of various data kinds. Pair offers a technique to keep two disparate objects together in storage. Essentially, we use it to store tuples. A pair container is a straightforward container that is specified in the header and contains two data elements or objects.

  • The sequence is fixed, and the first element is referred to as "first" and the second as "second" (first, second).
  • It is possible to assign, copy, and compare a pair. The array of objects allocated in a map or hash map is by default of type "pair", meaning that all of the "first" components are distinct keys connected to objects with their "second"
  • We utilize the variable name, the dot operator, the word first or second, and the element we want to access.

Syntax for pair in C++:

Example:

A program for pair in C++;

Output

200 J

Pair Initialization:

A pair can also be initialized. You can use the following syntax to initialize pair.

Syntax:

Various approaches to pair initialization:

The make pair() function can also be used to initialize a pair.

Pair declaration may also use the following syntax:

Example:

Output

JavaTpoint 2.23

Note: If the pair's first value is not initialized, it is initialized automatically.

Example:

Output

00

Member Functions

1) make_pair():

Without explicitly defining the types, a value pair can be created using this template function.

Syntax:

Example:

Output

200 J
JavaTpoint 1.23
JavaTpoint is super 4.56

2) swap:

The contents of two pair objects are switched with the use of this function. Pairs must belong to the same category.

Syntax:

Example:

Output

content before swapping:
Contents of pair1 = P 1Contents of pair2 = Q 2
content after swapping:
Contents of pair1 = Q 2Contents of pair2 = P 1

3) tie():

This tie() method works the same as tuples work. To unpack the tuple (or pair in this case) values into independent variables, it produces a tuple of lvalue pointers to its parameters. Here are two variations of the tie, one with and one without "ignore", just like in tuples. The keyword "ignore" prevents a certain tuple element from being unpacked.

Pairs only have two arguments, but tuples can have numerous arguments. Therefore, unpacking needs to be dealt with explicitly in the case of pairs.

Syntax:

Example:

Output

10 12
16 12
16 18 p






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA