STL's Pair

Source: Internet
Author: User

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.

How to assign a pair:

1) Initialize:pair<string,double> fruit1 when created ("Orange", 4.5);//initialization

2) Assign the same value as the struct assignment:

Pair<string,double> Fruit2;
struct initialization
fruit2.first= "Banana";
fruit2.second=3.25;

3) Use Make_pair template function (header file #include<utility>):

Pair<string,double> fruit3;

Fruit3=make_pair ("Apple", 5.2);
struct initialization
fruit2.first= "Banana";
fruit2.second=3.25;

Other than that:

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

pair<string, string> A ("Lily", "Poly");  

string name;

name = Pair.second;

    • Create a new Pair object

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);


Template functions are defined in #include <iostream> #include <utility>//<utility> make_pair#include<string>using Namespace Std;void  Main () {pair<string,double> fruit1 ("Orange", 4.5);//Initialize Pair<string,double> Fruit2;pair<string,double> fruit3;//structure Initialization fruit2.first= "banana"; fruit2.second=3.25;    Assigning a pair structure using a template function Fruit3=make_pair ("Apple", 5.2);cout<< "pair:" <<endl;cout<< "The Price of" < <fruit1.first<< ' is $ ' <<fruit1.second<<endl;cout<< ' the price of ' <<fruit2.first << ' is $ ' <<fruit2.second<<endl;cout<< ' the price of ' <<fruit3.first<< ' is $ ' < <fruit3.second<<endl;}
the results of the operation are as follows:

Pair and Make_pair comparison:
Make_pair constructs a pair object.
Template
Pair Make_pair (T1 x, T2 y)
{
Return pair (x, y);
}
EG:STD::p Air ("sn001", 12.5);
Std::make_pair ("sn001", 12.5);
The two effects are the same.
if: std::p Air ("sn002", 12.6); 12.6 ' s datatype is float
Std::make_pair ("sn002", 12.6); 12.6 ' s datatype is double
Use:
std::p air M_paira;
M_paira = Std::make_pair ("sn001", 12.5);
std::cout<<m_paira.first<< "" <<m_pairA.second<<std::endl;
Combined with the simple use of map (the element type in map is pair):
std::p air M_paira;
M_paira = Std::make_pair ("sn001", 12.5);
std::cout<<m_paira.first<< "" <<m_pairA.second<<std::endl;
Std::map M_mapa;
M_mapa.insert (M_paira);
Std::map::iterator iter = M_mapa.begin ();
std::cout<<iter->first<< "" <<iter->second<<std::endl;Summary:Make_pair creates a pair object. It is convenient to use, for the data appearing in pairs, such as the book's ISBN corresponds to a title. pair is a single data pair operation, pair is a struct type, there are two member variables, accessed through First,second, with "." Access. map is an associative container in which the key-value pairs are stored, each element in the container is a pair type, the insert (pair type) is inserted by the map's insert () method, the iterator is used, and the value is "--". part of the content reference: http://blog.csdn.net/wangran51/article/details/8853565

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

STL's Pair

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.