473,387 Members | 1,700 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,387 software developers and data experts.

maps and constructors

Hello,
I am attempting to design a class:
class C {
std::map<double, const Ama;
std::map<double, const Bma;
..
public:
C(...);
};
The keys in the 2 maps are always identical. So I could choose:
std::map<double, const std::pair<A,B
or
std::map<double, const std::pair<A,B>* >
(ps: AFAIK, using a const-ref for the pair is impossible, but possible with
some boost classes)
Q1:
Is there a difference in memory usage, access speed or code
readability between the top choice and bottom ones?

Q2:
Between the 2 bottom ones (storing the pair object in the map vs storing the
ptr)
Q3:
For users of class C, what sort of constructors should I offer?
C::C(const std::map<double, const std::pair<A,B>* >& )
Or, should I allow adding elements to the map after the object being
constructed?
C::add(double, const A&, const B&)

Rds,
Jan 15 '08 #1
6 1938
Hicham Mouline wrote:
Hello,
I am attempting to design a class:
class C {
std::map<double, const Ama;
std::map<double, const Bma;
.
public:
C(...);
};
The keys in the 2 maps are always identical. So I could choose:
std::map<double, const std::pair<A,B
or
std::map<double, const std::pair<A,B>* >
(ps: AFAIK, using a const-ref for the pair is impossible, but
possible with some boost classes)
Q1:
Is there a difference in memory usage, access speed or code
readability between the top choice and bottom ones?
There is no "bottom one" to speak of. What does the pointer
point to? Without knowing that, it's impossible to tell you
the difference in memory usage, performance, or anything else.
Q2:
Between the 2 bottom ones (storing the pair object in the map vs
storing the ptr)
Same story. You don't explain where the pointer comes from, how
can we tell the difference?
Q3:
For users of class C, what sort of constructors should I offer?
C::C(const std::map<double, const std::pair<A,B>* >& )
You should offer what the users want. Ask the users. Since
you should synchronize the 'maps' it would be preferred to add
both 'A' and 'B' at the same time. Whether the users are going
to want to create a 'C' object from a pre-defined 'std::map',
I am not sure. Isn't the fact that you store those in a map
a simple implementation detail? It shouldn't dictate the user
interface to your class, you know.
Or, should I allow adding elements to the map after the object being
constructed?
C::add(double, const A&, const B&)
That seems preferable.

The users might also have arrays of 'double', 'A', and 'B', from
which they might want you to create your 'C' object. Consider
offering

C::C(size_t N, double* keys, const A* v1, const B* v2);

and tell them to make sure those arrays should better have at
least N elements. Then create your map in a loop, for example.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 15 '08 #2
On Jan 15, 10:19*am, "Hicham Mouline" <hic...@mouline.orgwrote:
Hello,

I am attempting to design a class:

class C {
* std::map<double, const Ama;
* std::map<double, const Bma;
.
public:
* C(...);

};

The keys in the 2 maps are always identical. So I could choose:

std::map<double, const std::pair<A,B
or
std::map<double, const std::pair<A,B>* >
(ps: AFAIK, using a const-ref for the pair is impossible, but possible with
some boost classes)

Q1:
Is there a difference in memory usage, access speed or code
readability between the top choice and bottom ones?
Using one map would be more efficient and would avoid the
synchronization problems already mentioned. You might consider
using a rb_tree as well. I wrote about that once
http://www.seventy7.homelinux.net/rbtree

Brian Wood
Ebenezer Enterprises

Jan 15 '08 #3
On 2008-01-15 17:19, Hicham Mouline wrote:
Hello,
I am attempting to design a class:
class C {
std::map<double, const Ama;
std::map<double, const Bma;
.
public:
C(...);
};
The keys in the 2 maps are always identical. So I could choose:
std::map<double, const std::pair<A,B
or
std::map<double, const std::pair<A,B>* >
(ps: AFAIK, using a const-ref for the pair is impossible, but possible with
some boost classes)
Q1:
Is there a difference in memory usage, access speed or code
readability between the top choice and bottom ones?
Yes, if you use two maps you have to store the key twice, and when
retrieving items you have to do the lookup twice too. If you always
handle both A and B together there is not reason to keep them in
separate maps.

Consider also the implications on exception-safety, if you have two maps
and want to insert a new pair of A and B and you successfully insert the
A into its map but an exception is thrown when inserting B you must
remove the A from its map.
Q2:
Between the 2 bottom ones (storing the pair object in the map vs storing the
ptr)
Yes, at the very least you need the extra memory that is used to store
the pointers in addition to the space for the pair. Of course you might
be able to save memory using the pointer if multiple entries in the map
refer to the same pair of A and B.
Q3:
For users of class C, what sort of constructors should I offer?
C::C(const std::map<double, const std::pair<A,B>* >& )
Or, should I allow adding elements to the map after the object being
constructed?
C::add(double, const A&, const B&)
Depends on how you plan to use C, perhaps you should offer both?

--
Erik Wikström
Jan 15 '08 #4
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fm**********@news.datemas.de...
> std::map<double, const Ama;
std::map<double, const Bma;
What are these consts doing here? The value type of a map is supposed to be
assignable.
>The keys in the 2 maps are always identical. So I could choose:
std::map<double, const std::pair<A,B
or
std::map<double, const std::pair<A,B>* >
std::map<double, std::pair<A, B

Why not?
Jan 15 '08 #5

"Andrew Koenig" <ar*@acm.orgwrote in message
news:KT*********************@bgtnsc04-news.ops.worldnet.att.net...
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fm**********@news.datemas.de...
>> std::map<double, const Ama;
std::map<double, const Bmb;

What are these consts doing here? The value type of a map is supposed to
be assignable.
my mistake.
>>The keys in the 2 maps are always identical. So I could choose:
std::map<double, const std::pair<A,B M
or
std::map<double, const std::pair<A,B>* MM

std::map<double, std::pair<A, B

Why not?
The const was my mistaken attempt to change the elements of the pair, ie. to
make the 2nd line of code
M[5.0] = std::pair<a1, b1>;
M[5.0].first = a2;
impossible
My point is to store the pairs in the map once, and then never change them
at a later stage.

The choice between std::map<double, std::pair<A, B and std::map<double,
const std::pair<A, B>* >:
With a million entries in the map, and if the pairs already exist elsewhere,
the non-ptr-version of the map would copy those
pairs, while the ptr version would store only a ptr to each pair. Here i
assume

sizeof( const std::pair<A, B>* ) < sizeof( std::pair<A, B)
Jan 16 '08 #6
Hicham Mouline wrote:
>
"Andrew Koenig" <ar*@acm.orgwrote in message
news:KT*********************@bgtnsc04-news.ops.worldnet.att.net...
>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fm**********@news.datemas.de...
>>> std::map<double, const Ama;
std::map<double, const Bmb;

What are these consts doing here? The value type of a map is supposed to
be assignable.
my mistake.
>>>The keys in the 2 maps are always identical. So I could choose:
std::map<double, const std::pair<A,B M
or
std::map<double, const std::pair<A,B>* MM

std::map<double, std::pair<A, B

Why not?
The const was my mistaken attempt to change the elements of the pair, ie.
to make the 2nd line of code
M[5.0] = std::pair<a1, b1>;
M[5.0].first = a2;
impossible
My point is to store the pairs in the map once, and then never change them
at a later stage.
Then, you should consider

typedef std::pair< A, B EntryType;
std::vector< tripple< std::pair< double, EntryType the_table;

Once all entries are stored, just sort() by the double entry and then use
binary_search() to retrieve values.
>
The choice between std::map<double, std::pair<A, B and std::map<double,
const std::pair<A, B>* >:
With a million entries in the map, and if the pairs already exist
elsewhere, the non-ptr-version of the map would copy those
pairs, while the ptr version would store only a ptr to each pair. Here i
assume

sizeof( const std::pair<A, B>* ) < sizeof( std::pair<A, B)

Best

Kai-Uwe Bux
Jan 16 '08 #7

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

Similar topics

4
by: Jeff Sandys | last post by:
I'm trying to write a mapping function for genealogy. I want to read a gedcom database and plot an icon at a geographic location based on a user's query. Can you help me find: 1) A python...
4
by: Stanimir Stamenkov | last post by:
I have this kind of construct: http://www.geocities.com/stanio/temp/usemap01.html (an IMG element using MAP described with AREA elements) I'm trying to make it more accessible and currently...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
3
by: Sean | last post by:
Have you ever wanted to add the great features inherent in Google Maps? Here is how you do it. ============== == STEP ONE == ============== Create a new MS Access form called frmGoogleMap....
5
by: roberts.noah | last post by:
It is my understanding that if you contain vectors or maps you don't need to create copy constructors because the default calls that constructor. It is my understanding that if you use these types...
2
by: rn5arn5a | last post by:
I am not sure where I should have posted this question in this newsgroup. Please excuse me if I am wrong. Nowadays, a lot of websites have come with Maps (Google Maps being an example). Can...
3
by: Phil Stanton | last post by:
I have a button on a form which when pressed displays a google map of the address. Code is Private Sub Googlemap_Click() MakeURL ("") End Sub
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
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...

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.