473,788 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::pair<,>


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 on what you are using the pair
for. For instance if I am using coordinates in two
dimensional space then I like to use x and y. So
I might as well define my own struct with x and
y members in it and create a constructor so
that I can easily instantiate pairs.

I wonder if there is a way to create a pair class
using std::pair but typedef its first and second
to x and y using C++. The only way I can think
of is to subclass std::pair<,>.

Suggestions and reccomendation on the best
practices and conventions for using std::pair<,>
are most welcome.

Thanks,

Neil

Jul 19 '05 #1
14 45869

Here is my way of making use of std::pair<class T1, class T2>:

#include <iostream>
#include <utility>

template<class T1, class T2>
class Coordinate: public std::pair<T1, T2> {
public:
Coordinate(T1 x, T2 y): std::pair<T1, T2>(x, y), x(first), y(second) { }
T1 &x;
T2 &y;
};

int main() {
Coordinate<int, int> foo(1, 2);
std::cout << "x = " << foo.x << "\ny = " << foo.y << std::endl;
}
Suggestions and reccomendation on the best
practices and conventions for using std::pair<,>
are most welcome.

Thanks,

Neil


Jul 19 '05 #2
> I would like to ask how come the design of C++ includes
std::pair.
I think primarly for std::map and the map-like containers.
First of all I don't think many programmers
would use it.
Depends on the programmer, I think. I use it quite
often.
For starters, what the first and second
members are depends on what you are using the pair
for. For instance if I am using coordinates in two
dimensional space then I like to use x and y. So
I might as well define my own struct with x and
y members in it and create a constructor so
that I can easily instantiate pairs.
Yes. std::pair<> was not created specifically to replace
every ud structs representing two entities.
I wonder if there is a way to create a pair class
using std::pair but typedef its first and second
to x and y using C++. The only way I can think
of is to subclass std::pair<,>.
That would be one way if you want your struct to be
compatible with std::pair<>. If not, make it
a private member or, as you said, create a new
struct.
Suggestions and reccomendation on the best
practices and conventions for using std::pair<,>
are most welcome.


There are none, imho.
Jonathan
Jul 19 '05 #3
Neil Zanella wrote:

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.

I generally use std::make_pair( ).


Brian Rodenborn
Jul 19 '05 #4
Neil Zanella wrote:

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 on what you are using the pair
for. For instance if I am using coordinates in two
dimensional space then I like to use x and y. So
I might as well define my own struct with x and
y members in it and create a constructor so
that I can easily instantiate pairs.

I wonder if there is a way to create a pair class
using std::pair but typedef its first and second
to x and y using C++. The only way I can think
of is to subclass std::pair<,>.

Suggestions and reccomendation on the best
practices and conventions for using std::pair<,>
are most welcome.


I think you can start by doing something like

template <typename T>
class Point : public std::pair<T,T>
{
public:
T& x,y;
Point(const T& a, const T& b) : std::pair<T,T>( a,b), x(first),
y(second) {}
/* ... */
};

This gives you more of a pointy interface while also modeling a
container.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #5
WW
Neil Zanella wrote:
Here is my way of making use of std::pair<class T1, class T2>:

#include <iostream>
#include <utility>

template<class T1, class T2>
class Coordinate: public std::pair<T1, T2> {
public:
Coordinate(T1 x, T2 y): std::pair<T1, T2>(x, y), x(first),
y(second) { } T1 &x;
T2 &y;
};


The above class cannot be assigned, or copy constructed. Plus: do not use
inheritance when composition suffices:

template<class T1, class T2>
struct Coordinate {
Coordinate():
store(T1(),T2() ), x(store.first), y(store.second) { }
Coordinate(T1 px, T2 py):
store(px,py), x(store.first), y(store.second) { }
Coordinate(Coor dinate const &o):
store(o.store), x(store.first), y(store.second) { }
Coordinate operator=(Coord inate const &o)
{ store = o.store; }
T1 &x;
T2 &y;
private:
std::pair<T1, T2> store;
};

Also note that the above class will be bigger than the pair. In case of
int, on a usual architecture it will be twice as big. So IMHO you are
better off having accessor functions - if you must access the elements from
the outside.

template<class T1, class T2>
struct Coordinate {
Coordinate(): store(T1(),T2() ) { }
Coordinate(T1 px, T2 py): store(px,py) { }
Coordinate(Coor dinate const &o): store(o.store) { }
Coordinate operator=(Coord inate const &o) { store = o.store; }
T1 &x() {return store.first; }
T2 &y() {return store.second; }
private:
std::pair<T1, T2> store;
};

But to be honest I would just leave std::pair out of this completely and
make a little template struct of my own, with the right names.

--
WW aka Attila
Jul 19 '05 #6
"Neil Zanella" <nz******@cs.mu n.ca> wrote in message
news:Pi******** *************** *************** @garfield.cs.mu n.ca...

Hello,

I would like to ask how come the design of C++ includes
std::pair.
It's useful for some things. If you don't find it
useful, don't use it.

Also note that the standard container types 'std::map'
and 'std::multimap' use type 'std::pair' objects to represent
their elements. If you use a map or multimap, you're
(at least indirectly) using 'std::pair' objects.
Also e.g. one of the overloads of 'std::map::inse rt()'
returns a 'std::pair' object (not an element value,
but a pair of values, the first of which is an iterator
object pointing to the inserted element (if one was inserted),
and the other a type 'bool' indicating whether or not
the insertion occurred.)

'std::pair' can also be used as a 'general purpose'
set of two values.
First of all I don't think many programmers
would use it.
I do.
For starters, what the first and second
members are depends on what you are using the pair
for.
Well of course.
For instance if I am using coordinates in two
dimensional space then I like to use x and y.
But what are their types? Are the types the same?
The already existing template 'std::pair' will
handle them whatever they are.
So
I might as well define my own struct with x and
y members in it and create a constructor so
that I can easily instantiate pairs.
This can already be done with 'std::pair', which
has three constructors defined (one of them a
template). "Don't reinvent the wheel" and all that.

I wonder if there is a way to create a pair class
using std::pair but typedef its first and second
to x and y using C++.
Are 'x' and 'y' types or values? In either case,
std::pair can handle it.

The only way I can think
of is to subclass std::pair<,>.
IMO no reason to.

std::pair<T,T>( x,y);

Done. :-)

Also see 'std::make_pair ()'


Suggestions and reccomendation on the best
practices and conventions for using std::pair<,>
are most welcome.


Use it if useful, don't use it if not.

-Mike
Jul 19 '05 #7
Mike Wahler wrote:
I wonder if there is a way to create a pair class
using std::pair but typedef its first and second
to x and y using C++. [snip]The only way I can think
of is to subclass std::pair<,>.


IMO no reason to.

std::pair<T,T>( x,y);


The question is, is a Point a pair? From the description of pair, it
seems like the answer is yes. So, if you want a Point with members x and
y, why not subclass?

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #8
David Rubin wrote:

[snip]
This gives you more of a pointy interface while also modeling a
container.


Not true; std::pair does not model a Container.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #9

"David Rubin" <bo***********@ nomail.com> wrote in message
news:3F******** *******@nomail. com...
Mike Wahler wrote:
I wonder if there is a way to create a pair class
using std::pair but typedef its first and second
to x and y using C++. [snip]The only way I can think
of is to subclass std::pair<,>.
IMO no reason to.

std::pair<T,T>( x,y);


The question is, is a Point a pair? From the description of pair, it
seems like the answer is yes.


Agreed.
So, if you want a Point with members x and
y, why not subclass?


Why not simply:

std::pair<int,i nt> coord(x,y);

or if you like:

typedef std::pair<int,i nt> Point;

Point coord(x,y);

??

-Mike
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
6165
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
6
2754
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
6
1518
by: asdf | last post by:
It is best to show me some examples. Thanks a lot.
2
7284
by: subramanian100in | last post by:
Consider the following piece of code: #include <iostream> #include <fstream> #include <vector> #include <string> #include <utility> #include <iterator> #include <algorithm> int main()
18
2510
by: subramanian100in | last post by:
Consider a class that has vector< pair<int, string>* c; as member data object. I need to use operator>to store values into this container object and operator<< to print the contents of the container. I have written both these operators as non-friend functions.
9
3725
by: shaun roe | last post by:
Question about pointer-to-data members I have a deeply nested loop, the innermost bit looks a little like this: for(it(vec.begin(), end(vec.end()), it!=end;++it){ if (global == 0){ myObj = it->func().second->anObj(); } else { myObj = it->func().first->anObj(); }
10
3791
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
10366
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.