Before learning about associative containers, first look at the pair class template in STL, because some of the member function return values of the associative container are pair objects, and the elements in map and Multimap are pair objects.
1) Pair class template definition
When the pair is instantiated, the class has two member variables, one is first and the other is second.
There is also a make_pair () function template in the STL that can return a pair template object. The source code is as follows:
Template<class T1, Class t2>
Pair<t1,t2>make_pair (T1 x, T2 y)
{
Return (pair<t1,t2> (x, y);
}
2) use of pair and Make_pair
//Program 19.4.1.cpp pair and Make_pair usage:#include <iostream>using namespacestd;intMain () {pair<int,Double>P1; cout<< P1.first <<","<< P1.second << Endl;//output 0,0pair<string,int> P2 (" This", -); cout<< P2.first <<","<< P2.second << Endl;//Output this,20pair<int,int> P3 (pair<Char,Char> ('a','b'));//initialized with the third constructor (the third constructor is a function template, the argument is a reference to a pair class object),//Therefore, a temporary object is generated to initializecout << P3.first <<","<< P3.second << Endl;//Output 97,98pair<int,string> P4 = Make_pair ( $,"Hello"); cout<< P4.first <<","<< P4.second << Endl;//Output 200,hello return 0;}
Note Long notes
4.1 Pair class Template