Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 107959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:50:11+00:00 2026-05-11T01:50:11+00:00

Please consider the following scenario: map(T,S*) & GetMap(); //Forward decleration map(T, S*) T2pS =

  • 0

Please consider the following scenario:

 map(T,S*) & GetMap(); //Forward decleration  map(T, S*) T2pS = GetMap();  for(map(T, S*)::iterator it = T2pS.begin(); it != T2pS.end(); ++it) {     if(it->second != NULL)     {         delete it->second;         it->second = NULL;     }     T2pS.erase(it);     //In VS2005, after the erase, we will crash on the ++it of the for loop.     //In UNIX, Linux, this doesn't crash. }//for 

It seems to me that in VS2005, after the ‘erase’, the iterator will be equal to end(), hence the crash while trying to increment it. Are there really differences between compilers in the behavior presented here? If so, what will the iterator after the ‘erase’ equal to in UNIX/Linux?

Thanks…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:50:11+00:00Added an answer on May 11, 2026 at 1:50 am

    Yes, if you erase an iterator, that iterator gets a so-called singular value, which means it doesn’t belong to any container anymore. You can’t increment, decrement or read it out/write to it anymore. The correct way to do that loop is:

    for(map<T, S*>::iterator it = T2pS.begin(); it != T2pS.end(); T2pS.erase(it++)) {     // wilhelmtell in the comments is right: no need to check for NULL.      // delete of a NULL pointer is a no-op.     if(it->second != NULL) {         delete it->second;         it->second = NULL;     } } 

    For containers that could invalidate other iterators when you erase one iterator, erase returns the next valid iterator. Then you do it with

    it = T2pS.erase(it) 

    That’s how it works for std::vector and std::deque, but not for std::map or std::set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 263k
  • Answers 263k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One strategy might be to convert the trunk. You might… May 13, 2026 at 11:56 am
  • Editorial Team
    Editorial Team added an answer There is Quince for example. A large library of UX… May 13, 2026 at 11:56 am
  • Editorial Team
    Editorial Team added an answer I came up with this solution. I think your idea… May 13, 2026 at 11:56 am

Related Questions

Please consider the following scenario: map(T,S*) & GetMap(); //Forward decleration map(T, S*) T2pS =
While migrating some code to the latest version of F#, included in VS2010 b1,
Please consider the following scenario in BizTalk 2006: Received message should be routed to
Consider the following database tables: Table messages with 13,000,000 rows (one row per message).
Surprisingly I was only able to find one previous question on SO about this

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.