Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 10th, 2008, 6:36 PM   #1
Generic User #2
Newbie
 
Join Date: Apr 2008
Posts: 6
Rep Power: 0 Generic User #2 is on a distinguished road
simple questions about maps

sorry to ask such a simple question, but....

what happens when you try to retrieve the value from a key that was never inserted into a map container?

also, if i delete a map container and all the values are pointers, will the referenced objects be deleted too?


i would just test this out normally, but i'm having 'technical issues' with my main comp and the one i'm currently using is prehistoric.


also, another Q:

somewhat concerning the assignment operator and more so with custom data structures.

in the situation of a data structure object being assigned the value of another data structure object, what would happen if the left-hand object returned a null value? what if the right-hand object was returned a null value?

sorry if i'm being too vague, i'll provide more details if necessary.
Generic User #2 is offline   Reply With Quote
Old May 10th, 2008, 7:21 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: simple questions about maps

I'm sorry if this comes off as being unhelpful, but this is a skill you could quite obviously benefit from: experimentation.

All of these questions you could answer through experimenting with little snippets of code. Even though you can always reference the documentation or ask for help, experimentation can sometimes be faster!

For example, for your first question, write a simple program that tries to access a non-existent item in a map. If I recall correctly, it should return 0 or NULL.

Using this skill effectively can often save you a lot of time and frustration when you aren't so sure about how something works. It also eliminates the need to elaborate or clarify in response to anyone's misunderstanding of your problem(s).

Edit: I just noticed that you said you're having technical issues with your main comp. Well then. Nevermind. But it still makes sense to experiment with all of these things once you get a chance (it sounds like you won't be coding until then either way).
Sane is offline   Reply With Quote
Old May 11th, 2008, 3:18 PM   #3
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: simple questions about maps

using the operator[] to access a key in a map will create that key. the value returned will depend on the value type. it will use the default constructor to create the value type. if a map contains pointers, it will not automatically delete the objects referenced by those pointers.
mbd is offline   Reply With Quote
Old May 12th, 2008, 7:41 PM   #4
Generic User #2
Newbie
 
Join Date: Apr 2008
Posts: 6
Rep Power: 0 Generic User #2 is on a distinguished road
Re: simple questions about maps

um...is there any way to prevent the operator[] from creating a key?? or is there a similiar data structure to a map?

also, not related, but how do you put in the 'line-numbering' on the 'margins' of the main text window in visual c++ 2008?
Generic User #2 is offline   Reply With Quote
Old May 13th, 2008, 12:42 AM   #5
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: simple questions about maps

you can check if a key exists using the find method.
if (mymap.find("test") != mymap.end())
{
  // we found it
}
else
{
  // we did not find it
}
mbd is offline   Reply With Quote
Old May 13th, 2008, 6:22 AM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: simple questions about maps

Sorry to hijack, but this could explain why maps haven't been working well for me. It seems that find runs in logarithmic time. Does the operator[] take logarithmic time as well? Under amortized analysis, could it ever be constant?
Sane is offline   Reply With Quote
Old May 13th, 2008, 10:38 AM   #7
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: simple questions about maps

searching for a key takes log(n) in a map. it would easily be possible to implement map as a hash table, but that will have to wait for a new standard. sgi provides a hash_map.
mbd is offline   Reply With Quote
Old May 13th, 2008, 11:18 AM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: simple questions about maps

Damn associative containers. Useless!

One of my algorithms was timing out because I was making 7,000,000 logarithmic calls to the STL map. No wonder. I'll need to avoid maps from now on. With a proper hash table, maps seem to be completely useless.
Sane is offline   Reply With Quote
Old May 13th, 2008, 6:49 PM   #9
Generic User #2
Newbie
 
Join Date: Apr 2008
Posts: 6
Rep Power: 0 Generic User #2 is on a distinguished road
Re: simple questions about maps

thanks for the help guys


ps those last few posts confused me. are you saying that as the number of elements increases, the marginal amount of time per subsequent element increases?
Generic User #2 is offline   Reply With Quote
Old May 13th, 2008, 6:57 PM   #10
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: simple questions about maps

Yes. But only very slightly. The slow down in lookup won't even be measurable until you get in the hundred's of thousands to millions of insertions within the span of a few seconds. But sometimes that is necessary. For most general applications, an STL map will be the simple and clean solution.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
hardware questions programmingnoob Coder's Corner Lounge 28 Aug 8th, 2006 8:04 PM
a simple linking loader for SIC/XE programmingnoob C++ 3 Feb 26th, 2006 11:35 PM
Novice questions. snafu Python 3 Oct 5th, 2005 5:03 PM
ucf cs student has some questions raspberryh Coder's Corner Lounge 33 Sep 12th, 2005 2:49 PM
Simple Function Questions meverha1 C++ 16 Sep 12th, 2005 1:25 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC