pair and make_pair

J

JustSomeGuy

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.insert(std::pair<Key, Value>(k,v));
works as well as:

dictionary.insert(std::make_pair<Key, Value>(k,v));
 
M

Mike Wahler

JustSomeGuy said:
in the stl map class I see the use of a function pair

'std::pair' is not a function, it's a type.
and make_pair.
What is the difference between pair and make_pair?

'std::pair' is a type, 'std::make_pair' is a function.
dictionary.insert(std::pair<Key, Value>(k,v));
works as well as:

dictionary.insert(std::make_pair<Key, Value>(k,v));


============================================================
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
 
G

Gianni Mariani

JustSomeGuy said:
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.insert(std::pair<Key, Value>(k,v));
works as well as:

dictionary.insert(std::make_pair<Key, Value>(k,v));

Sometimes Google is your friend:

http://www.google.com/search?hl=en&ie=UTF-8&q=make_pair+&btnG=Google+Search

http://www.sgi.com/tech/stl/pair.html

....
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).
 
D

Darkay Li

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top