STL's Pair Learning

Source: Internet
Author: User

http://blog.csdn.net/calvin_zcx/article/details/6072286

Http://www.linuxidc.com/Linux/2014-10/107621.htm

Header files: <utility>

Comparison rules for the pair's <, >, <=, >=, = =,! =: Compare second before comparing First,first (you can re-specify your own comparison logic by overloading these operators)

The initialization of pair: pair<string, int> prt;   Pair<string, INT>PR2 ("Hello", 5);    pair<string, int> PR3 = make_pair<string, int> ("haha", 4); PAIR<STIRNG, INT>PR4 = Make_pair ("lll", 3);

Pair and vector interaction: pair<string, vector<int>> student;

Pair access element: Pair<int, int> a (n);

cout<< "first=" <<a.first<< "------" << "second=" <<a.second;

Pair using typedef technique: typedef pair<string, int> Nameinfo;

Nameinfo info ("Lucy", 4);

Pair and standard input stream: pair<string, string> input;

while (Cin>>input.first>>input.second) {cout<< ' info is ' <<input.first<< ': ' << Input.second<<endl;}

(PS: Tips for using cin directly abc[enter]d---->info is abc:d)

The implicit conversion of pair: Pair can accept implicit type conversion, which can achieve higher flexibility. However, the following problems can occur: for example, there are two definitions:

Pair<int, float> (1, 1.1);
Make_pair (1, 1.1); The Make_pair function converts the second variable to a double type. The problem in programming is the need to draw attention.

Make_pair<int, float> (1,1.1); If you want to specify a float, you can indicate the type like this.

A small example of a vector applying a pair:

STL's Pair Learning

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.