1, the application of pair
A pair is a combination of two data into a set of data that can be used when such a requirement is needed, such as map in stl where key and value are stored together.Another application is to choose pair when a function needs to return two data.The implementation of pair is a structure, and the two main member variables are first second. Because struct is used instead of class, member variables of pair can be used directly.
Its standard library type, pair type, is defined in the #include <utility>header file as follows:
Class template: 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 (T1 and T2) into a single value.
This pair of values can have different data types (T1 and T2),
The two values can be accessed using pair's two public functions first and second, respectively.
Definition (constructor):
pair<T1, T2> p1; //Create an empty pair object (using the default construct) with two elements of type T1 and T2, initialized with values. pair<T1, T2> p1(v1, v2); //Create a pair object with two elements of type T1 and T2, where the first member is initialized to v1 and the second member is initialized to v2. make_pair(v1, v2); // Create a new pair object with the values of v1 and v2, whose element types are v1 and v2, respectively. p1 < p2; // A less-than operation between two pair objects whose definitions follow a dictionary order: for example, p1.first < p2.first or! (p2.first < p1.first) & (p1.second < p2.second) returns true. p1 == p2; // If the first and second of two objects are equal in turn, they are equal; the operation uses the element's==operator. p1.first; // Returns a public data member named first in object p1 p1.second; // Returns a public data member named second s in object p1
2, pair creation and initialization
A pair contains two values and, like a container, is a template type.However, it is different from the container described earlier.
When creating a pair object, you must provide two type names, which do not have to be the same type
pair<string, string> anon; // Create an empty object anon with both element types string pair<string, int> word_count; // Create an empty object, word_count, of type string and int, respectively pair<string, vector<int> > line; // Create an empty object line with two element types string and vector
Of course, member initialization can also occur at the time of definition:
pair<string, string> author("James","Joy"); // Create an author object with two element types of string and default initial values of James and Joy. pair<string, int> name_age("Tom", 18); pair<string, int> name_age2(name_age); // Copy Construction Initialization
The use of pair types is cumbersome, and if you define multiple objects of the same pair type, you can use typedef to simplify the declaration:
typedef pair<string,string> Author; Author proust("March","Proust"); Author Joy("James","Joy");
Assignment between variables:
pair<int, double> p1(1, 1.2); pair<int, double> p2 = p1; // copy construction to initialize object pair<int, double> p3; p3 = p1; // operator =
3, Operation of pair object
Accessing two element actions is accessible through first and sencond:
pair<int ,double> p1; p1.first = 1; p1.second = 2.5; cout<<p1.first<<' '<<p1.second<<endl; //Output: 1 2.5 string firstBook; if(author.first=="James" && author.second=="Joy") firstBook="Stephen Hero";
4, Generate a new pair object
You can also create a new pair object with make_pair:
pair<int, double> p1; p1 = make_pair(1, 1.2); cout << p1.first << p1.second << endl; //output: 1 1.2 int a = 8; string m = "James"; pair<int, string> newone; newone = make_pair(a, m); cout << newone.first << newone.second << endl; //output: 8 James
5, get pair element value through tie
When some sanitation functions return a pair object, it can be received directly through std::tie.For example:
std::pair<std::string, int> getPreson() { return std::make_pair("Sven", 25); } int main(int argc, char **argv) { std::string name; int ages; std::tie(name, ages) = getPreson(); std::cout << "name: " << name << ", ages: " << ages << std::endl; return 0; }