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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:28:45+00:00 2026-06-03T14:28:45+00:00

I wrote this small code just to see how an iterator actually gets invalidated

  • 0

I wrote this small code just to see how an iterator actually gets invalidated and does not point to changed location of a vector after its capacity is reached.

Here the size of vector and capacity is both 5 initially . After that I inserted a few other elements in vector and does not re-initialize my iterator to point to myvector.begin(). This lead to a junk value of 49 in my output after maximum size of vector is : 1073741823 when printing elements of vector again.

My question is why C++ does not make point iterator again to a valid myvector.begin() after all elements are copied into new location?
This can also lead to some behavior which can be hard to debug. I know a safe way to work would be to always reinitialize the iterator just before using it.

  #include<iostream>
  #include<vector>
  #include<stdio.h>

  using namespace std;

  int main()
  {
    vector<int> myvector;
    vector<int>::iterator it;
    int myarray[]= {100,200,300,400};
    myvector.insert(it,500);
    it=myvector.begin();
    myvector.insert(it,myarray,myarray+4);
    it=myvector.begin();
    for(;it!=myvector.end();++it)
    cout <<*it<<endl;
    cout <<"size of vector is :" << myvector.size() <<"\n"; 
    cout <<"capacity of vector is : " << myvector.capacity()<<"\n";  
    cout <<"maximum size of vector is : " << myvector.max_size()<<"\n"; 
    myvector.push_back(600);
    for(;it!=myvector.end();++it)
    cout <<*it<<endl;
  }
  Output of program :-
  100
  200
  300
  400
  500
  size of vector is :5
  capacity of vector is : 5
  maximum size of vector is : 1073741823
  49
  100
  200
  300
  400
  500
  600
  • 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-03T14:28:47+00:00Added an answer on June 3, 2026 at 2:28 pm

    Because it’s impractical, and probably impossible.

    Is the vector supposed to keep a list of all its iterators and them modify all of them as soon as an invalide-triggering method is called?

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

Sidebar

Related Questions

I think this question is best asked with a small code snippet I just
I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
I wrote this method to check if a page exists or not: protected bool
I wrote this tiny code: #include <stdio.h> int main() { size_t temp; temp =
I wrote this code in C# to encrypt a string with a key: private
I wrote a small queue class in C++, on Windows. Just to test, I'm
I've been working on this small piece of code that seems trivial but still,
This is Uday. I have a problem with the write performance with a small
I wrote this function for filling closed loop, pixvali is declared globally to store
I wrote this function to get the unread count of google reader items. function

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.