By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,111 Members | 1,640 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,111 IT Pros & Developers. It's quick & easy.

pair and make_pair

P: n/a
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));


Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"JustSomeGuy" <no**@nottelling.com> wrote in message
news:MwLGc.973059$Pk3.272416@pd7tw1no...
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
Jul 22 '05 #2

P: n/a
JustSomeGuy wrote:
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&i...=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).
Jul 22 '05 #3

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

"JustSomeGuy" <no**@nottelling.com> дʼ
news:MwLGc.973059$Pk3.272416@pd7tw1no...
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));



Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.