CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    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

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to check the result returned from an iterator

    [ Moved thread ]
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  4. #4
    Join Date
    Dec 2010
    Posts
    907

    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

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to check the result returned from an iterator

    Quote Originally Posted by lucky6969b View Post
    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

  6. #6
    Join Date
    Dec 2010
    Posts
    907

    Re: How to check the result returned from an iterator

    Sorry, i know. uninitiailized var. Too careless...

  7. #7
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    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.

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How to check the result returned from an iterator

    Quote Originally Posted by Yves M View Post
    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.

  9. #9
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: How to check the result returned from an iterator

    Quote Originally Posted by lucky6969b View Post
    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

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to check the result returned from an iterator

    Quote Originally Posted by Richard.J View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured