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

  • SEARCH
  • Home
  • 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 7856727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:48:10+00:00 2026-06-02T20:48:10+00:00

Please correct me if I am wrong. Thank you! insert and erase will relocate

  • 0

Please correct me if I am wrong. Thank you!

insert and erase will relocate elements, but elements before the position where insertion/erasure takes place don’t relocate and hence their iterators remain valid.

push_back and pop_back don’t invalidate any iterators.

push_front and pop_front invalidate all iterators.

swap won’t relocate elements, but somehow I think it should invalidate iterators.

  • 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-06-02T20:48:12+00:00Added an answer on June 2, 2026 at 8:48 pm

    push_back() and push_front() are defined in terms of insert(). Similarly, pop_back() and pop_front() are defined in terms of erase().

    Here’s what the C++03 standard says about iterator invalidation for insert() (23.2.1.3/1):

    An insert in the middle of the deque invalidates all the iterators and
    references to elements of the deque. An insert at either end of the
    deque invalidates all the iterators to the deque, but has no effect on
    the validity of references to elements of the deque.

    So push_front() and push_back() will invalidate iterators, but references to the elements themselves remain valid.

    For erase() at either end (23.2.1.3/4):

    An erase in the middle of the deque invalidates all the iterators and
    references to elements of the deque. An erase at either end of the
    deque invalidates only the iterators and the references to the erased
    elements.

    So pop_front() and pop_back() only invalidate iterators/references to the element at the end in question.

    And this is said says this about swap() for any standard container (23.1/10 "Container requirements"):

    no swap() function invalidates any references, pointers, or iterators
    referring to the elements of the containers being swapped.

    C++11 adds the following clarifications regarding how the end() iterator on a deque behaves for these operations. Basically, an iterator to end() should be treated as invalid after a swap() or after erasing the last element in the deque:

    An erase operation that erases the last element of a deque invalidates only the past-the-end iterator and all iterators and references to the erased elements.

    Every iterator referring to an element in one container before the swap shall refer to the same element in the other container after the swap. It is unspecified whether an iterator with value a.end() before the swap will have value b.end() after the swap.

    I think it would be a good idea to code as if these rules apply even if you’re not yet using a C++11 compiler.

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

Sidebar

Related Questions

Please correct me if I am wrong. I want to replace substring in a
Need quick help from win developers. Please correct me where I got it wrong.
Please, what is wrong with the following code? And what is the correct way
please correct the program. Q. Write a java prg to accept RollNo, Name of
Please let me know whether its correct or not? I am mapping the whole
Could you please explain why this code is not syntactically correct? private void addEditor(final
I am unable to get the correct title for this, please change the title
I've read somewhere that <img> element behaves like both. If correct, could someone please
Please help! I need to produce a Crystal Report with multiple data columns, but
This is probably a noob question that I will get slated for but here

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.