Application of 1 Pair
Pair is the combination of 2 data into one data, when the need for such a requirement can be used pair, such as the map in the STL is to put key and value together to save. Another application is when a function needs to return 2 data, you can choose pair. The implementation of the pair is a struct, the main two member variables are first second because the struct is not a class, so you can directly use the pair's member variable.
2 Make_pair function
Template pair Make_pair (T1 A, T2 b) {return pair (A, b);}
Obviously, we can use the pair's constructor or we can use Make_pair to generate the pair we need. General Make_pair are used in the need to do the parameters of the position, you can call Make_pair directly generated pair object is very convenient, the code is very clear. Another aspect of using the pair is that you can accept implicit type conversions, which gives you a higher degree of flexibility. Flexibility also brings a number of issues such as:
std::p air<int, float> (1, 1.1);
Std::make_pair (1, 1.1);
Is different, the first one is float, and the 2nd one will match itself into double.
Class templates: Template <class T1, class t2> struct pair
Parameter: T1 is the data type of the first value, and T2 is the data type of the second value.
Function: Pair combines a pair of values into a single value, which can have different data types (T1 and T2), and two values can be accessed using the two public functions first and second of the pair respectively.
Specific usage:
1. Definition (construction):
1 pair<int, double> p1; Use the default constructor 2 pair<int, double> p2 (1, 2.4); Initializes 3 Pair<int with the given value , double> p3 (p2); Copy constructor
2. Access two elements (via first and second):
1 pair<int, double> p1; Use default constructor 2 p1.first = 1;3 p1.second = 2.5;4 cout << p1.first << ' << p1.second << ; Endl
Output results: 1 2.5
3. Assignment operator =:
(1) Using Make_pair:
1 pair<int, double> p1;2 p1 = Make_pair (1, 1.2);
(2) Assigning values between variables:
Pair<int, double> p1 (1, 1.2); Pair<int, double> p2 = p1;
Pair Type Overview
Pair is a template type that contains two data values, and the two types of data can be different, and the basic definition is as follows:
Pair<int, string> A;
Indicates that there are two types in a, the first element is of type int, the second element is of type string, and if it is not initialized when the pair is created, the default constructor is called to initialize it.
Pair<string, string> A ("James", "Joy");
It can also be initialized directly to the definition at the same time as above.
Because the use of pair type is cumbersome, because if you want to define more than one pair type, the TypeDef simplifies the declaration:
typedef pair<string, string> author;
Author Pro ("May", "Lily");
Author Joye ("James", "Joyce");
Operation of the pair object
- For the pair class, because it has only two elements, named First and second, it can access its members directly using the normal dot operator
pair<string, String> A ("Lily", "Poly");
string name;
name = Pair.second;
You can use Make_pair to construct a new pair type for the two data that already exists:
int a = 8;
string m = "James";
Pair<int, string> Newone;
Newone = Make_pair (A, m);
Usage of C + + pair