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 5839873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:39:14+00:00 2026-05-22T11:39:14+00:00

STL containers have the problem that iterators can be invalidated when the container changes.

  • 0

STL containers have the problem that iterators can be invalidated when the container changes. Would it be possible for the container to announce that it has changed by adding a call has_changed()?

It is common to query empty() before certain operations. If the container set a bool on operations that would affect iterators like insert() or erase() then it would be possible to query has_changed() before reusing an iterator.

Crazy?

EDIT Thanks for a bunch of good replies and food for thought. I wish I could award more than one winner.

  • 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. Editorial Team
    Editorial Team
    2026-05-22T11:39:15+00:00Added an answer on May 22, 2026 at 11:39 am

    The (approximate) way fail-fast iterators work in Java is that the container increments a counter each time it changes. The iterators copy this counter on creation, and increment it each time the container is changed through them. If the iterator detects a mismatch, it throws an exception.

    C++ has the exciting property that some operations invalidate some iterators on the container, but not others. For example, assuming sufficient space has been reserved vector::insert invalidates iterators after the insertion point, but not before.

    Another difficult case is list::remove. This leaves all iterators valid except the one removed, and all copies of it.

    As you can imagine, it would be pretty difficult to track this precisely. What happens in practice is that your implementation might offer debug options in which iterators will do their best to detect whether they’re valid or not. This might depend on implementation details of whether they’ll currently “work”, though, rather than on whether the standard guarantees that they still work.

    It would be possible to do something in C++ similar to the “version number” in Java, but it would give false positives of iterators which appear invalid but in fact are valid.

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

Sidebar

Related Questions

No related questions found

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.