Thread: Iterator Confusion

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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