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);
Turn: C + + Learning pair