473,413 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

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

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


Jul 22 '05 #1
3 13770
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Neil Zanella | last post by:
Hello, I would like to ask how come the design of C++ includes std::pair. First of all I don't think many programmers would use it. For starters, what the first and second members are depends...
6
by: pmatos | last post by:
Hi all, Is there a way of (after creating a pair) set its first and second element of not? (pair is definitely a read-only struct)? Cheers, Paulo Matos
5
by: ma740988 | last post by:
For starters, Happy New Year to all!! I created a vector of pairs where pair first is a primitive and pair second is a vector of ints. So now: # include <iostream> # include <vector>
11
by: kietzi | last post by:
Hello world, I was wondering whether it would be possible to create a map which uses a pair of ints as key and a float as value. I have used maps with const char* as key, but have so far not been...
4
by: onkar | last post by:
#include<iostream> using namespace std; int main(void){ const pair<const char*,const char*arr={ pair<const char*,const char*>("1","1"), pair<const char*,const char*>("12","12"), pair<const...
18
by: desktop | last post by:
I have made this little test with std::pair: test<intt1(1); test<intt2(2); std::pair<test<int>,test<int mypair = std::make_pair(t1,t2); where test is a template class I wrote. It seems a bit...
3
by: eiji.anonremail | last post by:
Hi folks, I have a compile problem on linux, and maybe someone has an idea: #include <map> #include <iostream>
1
by: subramanian100in | last post by:
consider the program: #include <cstdlib> #include <iostream> #include <utility> using namespace std; class Test {
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.