-
February 7th, 2012, 11:02 AM
#1
How to check the result returned from an iterator
Code:
it = m_CoopTable->m_SparseMap.find(s);
if (it != NULL) //Error
{
return false;
}
This gives me compile-time error. it is an iterator to a hash_map
Thanks
Jack
-
February 7th, 2012, 11:05 AM
#2
Re: How to check the result returned from an iterator
-
February 7th, 2012, 11:08 AM
#3
Re: How to check the result returned from an iterator
You check against end(), eg, in this case, m_CoopTable->m_SparseMap.end(). This is in the hash_map::find() documentation.
-
February 8th, 2012, 04:36 AM
#4
Re: How to check the result returned from an iterator
Hello Lindley,
Thank you for your help. The info is very useful.
I have another trouble concerning with the use of hash_maps
My program crashes at a spot which has a hash_map that the whole map contains a long series of error items
It is shown as (error) | 0 in the intellisense window. I have cleared it out right at the start of the constructor.
(error) | 0
(error) | 0
(error) | 0
etc
Code:
stdext::hash_map<std::string, CObjects*> m_SparseMap;
Any more hints?
Thanks
Jack
-
February 8th, 2012, 05:15 AM
#5
Re: How to check the result returned from an iterator
Originally Posted by lucky6969b
I have another trouble concerning with the use of hash_maps. My program crashes at a spot which has a hash_map that the whole map contains a long series of error items
Your program has a bug.
You can't get an answer by posting just a declaration.
Regards,
Paul McKenzie
-
February 8th, 2012, 06:11 AM
#6
Re: How to check the result returned from an iterator
Sorry, i know. uninitiailized var. Too careless...
-
February 8th, 2012, 07:50 AM
#7
Re: How to check the result returned from an iterator
Code:
stdext::hash_map<std::string, CObjects*> m_SparseMap;
Just a mention that putting pointers to objects inside STL containers is not generally a good idea, precisely because you run into problems like uninitialized objects, memory leaks, double free etc. The only (defendable) use for it is when you somehow really need to use inheritance on the objects.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
February 8th, 2012, 08:27 AM
#8
Re: How to check the result returned from an iterator
Originally Posted by Yves M
Code:
stdext::hash_map<std::string, CObjects*> m_SparseMap;
Just a mention that putting pointers to objects inside STL containers is not generally a good idea, precisely because you run into problems like uninitialized objects, memory leaks, double free etc. The only (defendable) use for it is when you somehow really need to use inheritance on the objects.
Even then, you have no excuse not to make it a vector of unique_ptr, shared_ptr, or just plain use ptr_vector. EDIT: Oh wait, hash_map, not vector... in that case: ptr_unordered_map.
Last edited by monarch_dodra; February 8th, 2012 at 08:32 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
February 8th, 2012, 09:50 AM
#9
Re: How to check the result returned from an iterator
Originally Posted by lucky6969b
It is shown as (error) | 0 in the intellisense window. I have cleared it out right at the start of the constructor.
(error) | 0
(error) | 0
(error) | 0
Jack
Assuming you are using VisualStudio, this might be an issue that you are using the debugger on a release build. My experience is that in a release build the debugger is not able to inspect the content of the STL containers
-
February 8th, 2012, 11:30 AM
#10
Re: How to check the result returned from an iterator
Originally Posted by Richard.J
Assuming you are using VisualStudio, this might be an issue that you are using the debugger on a release build. My experience is that in a release build the debugger is not able to inspect the content of the STL containers
If optimization is disabled it should work okay.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|