4.1 Pair class Template

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.