pair and make_pair

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JustSomeGuy

    pair and make_pair

    in the stl map class I see the use of a function pair and make_pair.
    What is the difference between pair and make_pair?

    dictionary.inse rt(std::pair<Ke y, Value>(k,v));
    works as well as:

    dictionary.inse rt(std::make_pa ir<Key, Value>(k,v));






  • Mike Wahler

    #2
    Re: pair and make_pair

    "JustSomeGu y" <nope@nottellin g.com> wrote in message
    news:MwLGc.9730 59$Pk3.272416@p d7tw1no...[color=blue]
    > in the stl map class I see the use of a function pair[/color]

    'std::pair' is not a function, it's a type.
    [color=blue]
    > and make_pair.
    > What is the difference between pair and make_pair?[/color]

    'std::pair' is a type, 'std::make_pair ' is a function.
    [color=blue]
    > dictionary.inse rt(std::pair<Ke y, Value>(k,v));
    > works as well as:
    >
    > dictionary.inse rt(std::make_pa ir<Key, Value>(k,v));[/color]


    =============== =============== =============== ===============
    ISO/IEC 14882:1998(E)

    [....]

    20.2.2 Pairs

    1 The library provides a template for heterogeneous pairs of
    values. The library also provides a matching template function
    to simplify their construction.

    template <class T1, class T2>
    struct pair {
    typedef T1 first_type;
    typedef T2 second_type;

    T1 first;
    T2 second;
    pair();
    pair(const T1& x, const T2& y);
    template<class U, class V> pair(const pair< U, V> & p);
    };

    [....]

    template <class T1, class T2>
    pair<T1, T2> make_pair(const T1& x, const T2& y);

    7 Returns: pair<T1, T2>(x, y).

    8 [Example: In place of:

    return pair<int, double>(5, 3.1415926); // explicit types

    a C++ program may contain:

    return make_pair(5, 3.1415926); // types are deduced

    --end example]

    [....]
    =============== =============== =============== ===============

    -Mike


    Comment

    • Gianni Mariani

      #3
      Re: pair and make_pair

      JustSomeGuy wrote:[color=blue]
      > in the stl map class I see the use of a function pair and make_pair.
      > What is the difference between pair and make_pair?
      >
      > dictionary.inse rt(std::pair<Ke y, Value>(k,v));
      > works as well as:
      >
      > dictionary.inse rt(std::make_pa ir<Key, Value>(k,v));[/color]

      Sometimes Google is your friend:





      ....
      template <class T1, class T2>
      pair<T1, T2> make_pair(const T1& x, const T2& x)

      Equivalent to pair<T1, T2>(x, y). This is a global function, not a
      member function. It exists only for the sake of convenience.


      Anyhow - this means that you can write this:

      make_pair(3,"s" )

      instead of

      pair<int, const char *>(3, "s")

      because C++ will deduce template arguments of functions (but not
      non-template constructors of template classes).

      Comment

      • Darkay Li

        #4
        Re: pair and make_pair

        make_pair is an assistant function for create pair, it automatically deduce
        the type of pair's two parameters.

        "JustSomeGu y" <nope@nottellin g.com> дÈëÓʼþ
        news:MwLGc.9730 59$Pk3.272416@p d7tw1no...[color=blue]
        > in the stl map class I see the use of a function pair and make_pair.
        > What is the difference between pair and make_pair?
        >
        > dictionary.inse rt(std::pair<Ke y, Value>(k,v));
        > works as well as:
        >
        > dictionary.inse rt(std::make_pa ir<Key, Value>(k,v));
        >
        >
        >
        >
        >
        >[/color]



        Comment

        Working...