The usage of C++ pair

  • 2020-05-26 09:49:13
  • OfStack

An example of the usage of C++ pair

Application of 1 pair

pair is to combine two data into one data. When such a requirement is needed, pair can be used. For example, map in stl is to store key and value in 1. Another application is to select pair when a function needs to return 2 data. The implementation of pair is a structure, and the two main member variables are first second. Since struct is used instead of class, the member variables of pair can be directly used.

2 make_pair function


template pair make_pair(T1 a, T2 b) { return pair(a, b); }

Obviously, we can use pair's constructor or make_pair to generate the pair we need. 1. make_pair is used in places where pair is needed as a parameter. It is very convenient to directly call make_pair to generate pair objects, and the code is also very clear. Another aspect to use is that pair can accept implicit type conversions for greater flexibility. Flexibility also brings problems such as:


std::pair<int, float>(1, 1.1);

std::make_pair(1, 1.1);

The first one is float, and the second one will match itself to double.

Class template: template < class T1, class T2 > struct pair

Parameter: T1 is the data type of the first value, T2 is the data type of the second value.

Function: pair combines a pair of values into a single value that can have different data types (T1 and T2) and can be accessed by first and second, the two public functions of pair, respectively.

Specific usage:

1. Definition (construction) :


   pair<int, double> p1; // Use the default constructor 
   pair<int, double> p2(1, 2.4); // Initialize with a given value 
   pair<int, double> p3(p2); // Copy constructor 

2. Access to two elements (via first and second) :


   pair<int, double> p1; // Use the default constructor 
   p1.first = 1;
   p1.second = 2.5;
   cout << p1.first << ' ' << p1.second << endl;

Output result: 1 2.5

3. Assign operator = :

(1) use make_pair:


 pair<int, double> p1;
 p1 = make_pair(1, 1.2);

(2) assignment between variables:


  pair<int, double> p1(1, 1.2);
  pair<int, double> p2 = p1;

Pair type overview

pair is a template type, which contains two data values. The two data types can be different. The basic definition is as follows:


pair<int, string> a;

Means that there are two types in a, the first element is of type int and the second element is of type string. If pair was not initialized when it was created, the default constructor is called to initialize it.


pair<string, string> a("James", "Joy");

You can also initialize it directly at definition time, as in 1 above.

Because the use of pair type is tedious, if you want to define multiple pair types, you can simplify the declaration of typedef:


typedef pair<string, string> author;
author pro("May", "Lily");
author joye("James", "Joyce");

Operations on the Pair object

For the pair class, since it has only two elements, named first and second, its members can be accessed directly using the normal point operator


pair<string, string> a("Lily", "Poly"); 
string name;
name = pair.second;

Generate the new pair object

You can use make_pair to construct a new pair type on two existing data:


std::pair<int, float>(1, 1.1);

std::make_pair(1, 1.1);
0

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: