1. Application of pair
Pair is to combine two data into a group of data. When such requirements are needed, pair can be used. For example, map in stl is to save key and value together. Another application is to select pair when a function needs to return two data. The implementation of pair is a structure. The two main member variables are first second. Because struct is used instead of class, the member variables of pair can be used directly.
The 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 one value,
This pair of values can have different data types (T1 and T2),
The two values can be accessed by two public functions of pair, first and second.
Definition (constructor):
pair<T1, T2> p1; //Create an empty pair Object (using the default construction), whose two elements are T1 and T2 Type, initialized with a value.
pair<T1, T2> p1(v1, v2); //Create a pair Object whose two elements are T1 and T2 Type, where first Member initialized to v1,second Member initialized to v2.
make_pair(v1, v2); // with v1 and v2 Create a new pair Object whose element types are v1 and v2 Type of.
p1 < p2; // Two pair The definitions of less than operations between objects follow the dictionary order: e.g p1.first < p2.first perhaps !(p2.first < p1.first) && (p1.second < p2.second) Then return true.
p1 == p2; // If two objects first and second If they are equal in turn, the two objects are equal; The operation uses the of the element==Operator.
p1.first; // Return object p1 Middle name is first Public data member of
2. pair creation and initialization
Pair contains two values. Like a container, pair is also a template type. However, it is different from the container introduced earlier;
When creating a pair object, you must provide two type names. The types of the two corresponding type names do not have to be the same
pair<string, string> anon; // Create an empty objectanon,Both element types are string
pair<string, int> word_count; // Create an empty object word_count, The two element types are string and int type
pair<string, vector<int> > line; // Create an empty object line,The two element types are string and vector type
Of course, you can also initialize members during definition:
pair<string, string> author("James","Joy"); // Create a author Object, the two element types are string Type, and the default initial value is James and Joy.
pair<string, int> name_age("Tom", 18);
pair<string, int> name_age2(name_age); // Copy construct initialization
The use of pair types is quite cumbersome. 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
The operation of accessing two elements can be accessed through first and sencond:
pair<int ,double> p1;
p1.first = 1;
p1.second = 2.5;
cout<<p1.first<<' '<<p1.second<<endl;
//Output results: 1 2.5
string firstBook;
if(author.first=="James" && author.second=="Joy")
firstBook="Stephen Hero";
4. Generate a new pair object
You can also use make_pair create a new pair object:
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
Explain pair and make here_ The difference between pair
pair is a binary class template, make_pair is actually a function template for creating a binary. In C + +, the function template can omit parameters and make_pair also takes advantage of this feature.
Let's take a look at make_ Implementation of pair
template<class K,class V>
inline std::pair<K,V> make_pair(const K&k,const V&v)
{
return std::pair<K,V>(k,v);
}
You can see that make_pair actually calls the constructor of pair and makes_ Pair does not need to specify a type, but can be deduced directly
5. Get the pair element value through tie
When some clearing functions take the pair object as the return value, they 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;
}