Thread: Iterator Confusion

  1. #1
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147

    Iterator Confusion

    I'm sure once I see the solution to this problem I'll be banging my head on the desk but anyway, here goes.

    I created a std::map that I'm iterating through checking for a value. If the value matches, I delete the entry in map and continue on until I reach the end of the list.

    The problem I'm having is when I try to delete the last element in the map. It would make sense that there are no further elements after deleting it. A this point the program backs out with a debug error.

    I thought to add a check, if(it = map.end()) but I'm getting " conditional expression of type 'std::_Tree<_Traits>::iterator' is illegal" error. So clearly I'm using it incorrectly.

    Here's the block of code that I'm using. The only real relevant part is the for() loop iterating through mTextureArray which is a std::map:

    Code:
    for(map<Image*, pair<GLuint, int> >::iterator i  ; i != mTextureArray.end(); i++)
    {
    	if((currentTick - i->second.second) > TEXTURE_EXPIRE_TIME)
    	{
    		glDeleteTextures(1, &i->second.first);
    		mTextureArray.erase(i);
    	}
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by leeor_net
    I created a std::map that I'm iterating through checking for a value. If the value matches, I delete the entry in map and continue on until I reach the end of the list.
    Are you sure that a std::map is the correct container for this? Anyway, what you to do is this:
    Code:
    map<Image*, pair<GLuint, int> >::iterator i = mTextureArray.begin();
    while(i != mTextureArray.end())
    {
        if((currentTick - i->second.second) > TEXTURE_EXPIRE_TIME)
        {
            glDeleteTextures(1, &i->second.first);
            mTextureArray.erase(i++);
        }
        else
        {
            ++i;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    Quote Originally Posted by laserlight View Post
    Are you sure that a std::map is the correct container for this?
    No, not really. I asked about it yesterday and somebody suggested I use a std:: pair<>. The comparisons work but I don't know if there's a better solution.

    This now works as I expect it to. Now I'm interested in why it works like this so I can have a better understanding of how to properly use iterators.

    Thanks for the tip. I always appreciate your help!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by leeor_net
    This now works as I expect it to. Now I'm interested in why it works like this so I can have a better understanding of how to properly use iterators.
    When an element is erased from a std::map, iterators that refer to it are invalidated. Therefore we should advance the iterator before using it to erase the element, and post-increment works just right to do that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    Ah, I understand now. That makes perfect sense. I knew a bit about invalidating iterators but never thought I had come across it.

    Thank you!

Popular pages Recent additions subscribe to a feed