Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2005

Tip: Looking for answers? Try searching our database.

Possible problem with iterators in STL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
babak - 27 Sep 2005 09:46 GMT
Hi everyone
I have a problem with Iterators and Containers in STL that hopefully
someone can help me with.
This is what I try to do:

I have an associative (map) container and I have a function where I
with the help of iterators want to search through the container and
remove a certain object in the container.
Here is part of the code in that function:

//Move from timers map container to stale list container
Timers::iterator start(_timers.begin());
Timers::iterator last(_timers.end());

while (start != last)
{
    if(compObj.Compare(start->second))
    {
        _stale.push_back(start->second);

        start = _timers.erase(start);

    }
    else
    {
        ++start;
    }
}

The function works correctly most of the time (maybe 8 out of 10
times). It then goes through the while loop 3 times. The first time it
goes into the if case and erases the object and the other 2 times it
goes to the else case and then it jumps out of the while loop.
But sometimes it does not behave correctly. In those cases it behaves
like I described above until the third time in the while loop. The
function then goes to the else case but never leaves it (i.e it comes
to the line before ++start but not the line after it when I use
printfs).

I believe that all the initializations are made correctly so the only
thing I can come up with is that something goes wrong with the
iterator. Is there any possibility that I somehow e.g try to increase
the iterator out of bounds or make some other error which could lead to
very strange behaviour? Any other suggestions of what could lead to
this strange behaviour?

Thanks for your help.
Regards.
/Babak
Vladimir Nesterovsky - 27 Sep 2005 12:34 GMT
> //Move from timers map container to stale list container
> Timers::iterator start(_timers.begin());
[quoted text clipped - 16 lines]
>
> Any other suggestions of what could lead to this strange behaviour?

If it's not a problem with multithreading or with using map in different
dll, then I can think of code that works with _timers (within loop) that you
have omitted in your example.
Signature

Vladimir Nesterovsky
e-mail: vladimir@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com 


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.