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

Related Questions

Please consider the following scenario: Class Class1 Function Func() as String End Function End
Please consider following schema CREATE table articles ( id Int UNSIGNED NOT NULL AUTO_INCREMENT,
I want to create an android app. please consider the following scenario: Iam standing
Please consider the following scenario, There are two web applications App1 & App2. A
Please consider the following scenario in BizTalk 2006: Received message should be routed to
Please consider the following scenario. I have a form with a property: class MyForm
Please consider the following scenario with a simple HTML document and a JavaScript loaded
Please consider the following scenario: VNC Client try to connect with a VNC Server
please consider the following scenario for .net 2.0: I have an event that is
Please consider following sample table structure: +-------------------------------+--------------+------+-----+---------+-------+ | Field | Type | Null |

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.