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
Re: How to check the result returned from an iterator
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.
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
Re: How to check the result returned from an iterator
Quote:
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
Re: How to check the result returned from an iterator
Sorry, i know. uninitiailized var. Too careless...
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.
Re: How to check the result returned from an iterator
Quote:
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.
Re: How to check the result returned from an iterator
Quote:
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
Re: How to check the result returned from an iterator
Quote:
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.