Turn: C + + Learning 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.

Pair<string, string> A ("James", "Joy");

It can also be initialized directly to the definition at the same time as above.

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

  • For the pair class, because it has only two elements, named First and second, it can access its members directly using the normal dot operator

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

Turn: C + + Learning 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.