Pairs of STL

Source: Internet
Author: User

What is pair?

The introduction of pair classes is introduced in C ++ standard library as follows:

The classPairIs providedTreat two values as a single unit. It is used in several places within the C ++ standard library. In particle,The container classesMapAndMultimapUsePairS to manage their elements, Which are key/value pairs (see section 6.6). Another exampleThe usagePairsIs functions that return two values.

 

Pair Definition

The structure pair is defined in<Utility>As follows:

namespace std {    template <class T1, class T2>    struct pair     {        //type names for the values        typedef T1 first_type;        typedef T2 second_type;        //member        T1 first;        T2 second;        /* default constructor        * - T1 () and T2 () force initialization for built-in types        */        pair(): first(T1()), second(T2())         {        }        //constructor for two values        pair(const T1& a, const T2& b): first(a), second(b)         {        }        //copy constructor with implicit conversions        template<class U, class V>        pair(const pair<U,V>& p): first(p.first), second(p.second)         {        }    };    //comparisons    template <class T1, class T2>    bool operator== (const pair<T1,T2>&, const pair<T1,T2>&);    template <class T1, class T2>    bool operator< (const pair<T1,T2>&, const pair<T1,T2>&);        //similar: !=, <=, >, >=        //convenience function to create a pair    template <class T1, class T2>    pair<T1,T2> make_pair (const T1&, const T2&);}namespace std {    //comparisons    template <class T1, class T2>    bool operator== (const pair<T1,T2>& x, const pair<T1,T2>& y)     {        return x.first == y.first && x.second == y.second;    }    //similar: !=, <=, >, >=    template <class T1, class T2>    bool operator< (const pair<T1,T2>& x, const pair<T1,T2>& y)     {        return x.first < y.first || (!(y.first < x.first) && x.second < y.second);    }    //create value pair only by providing the values    template <class T1, class T2>    pair<Tl,T2> make_pair (const T1& x, const T2& y) {        return pair<T1,T2>(x, y);    }}

 

Use of pair

std::pair<int,float> p;    //initialize p. first and p.second with zerovoid f(std::pair<int,const char*>);void g(std::pair<const int.std::string>);void foo {    std::pair<int,const char*> p(42,"hello");    f(p);    //OK: calls built-in default copy constructor    g(p);    //OK: calls template constructor}std::make_pair(42, ‘@‘); //or std::pair<int,char>(42,‘@‘);void f(std::pair<int,const char*>);void g(std::pair<const int,std::string>);void foo {    f(std::make_pair(42,"hello"));     //pass two values as pair    g(std::make_pair(42,"hello"));     //pass two values as pair                                        // with type conversions}std::pair<int,float>(42,7.77);//does not yield the same asstd::make_pair(42,7.77);

The C ++ standard library uses pairs a lot.

For example,MapAndMultimapContainers usePairAs a type to manage their elements, which areKey/value pairs. See section 6.6, for a general description of maps and multimaps, and in particle page 91 for an example that shows the usage of TypePair.Objects of TypePairAre also used inside the C ++ standard library in functions that return two values (see page 183 For an example ).

Pairs of STL

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.