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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:29:24+00:00 2026-05-30T23:29:24+00:00

It doesn’t appear to (sample program), but can I be sure? // does resizing

  • 0

It doesn’t appear to (sample program), but can I be sure?

// does resizing an STL vector erase/invalidate it's previous contents?
#include <stdio.h>
#include <vector>
using namespace std ;

void print( vector<int>& t )
{
  for( int i = 0 ; i < t.size() ; i++ )
    printf( "%d ", t[i] ) ;
  puts("");
}

int main()
{
  vector<int> t ;
  t.resize( 12,9999 ) ;
  print(t) ;

  t.resize( 15, 10000 ) ;
  print(t) ;
}
  • 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-30T23:29:25+00:00Added an answer on May 30, 2026 at 11:29 pm

    Resizing an STL vector may require reallocating the underlying storage. This may cause any number of elements to be destroyed and recreated, and all iterators are invalidated. Accessing an invalidated iterator is a common source of errors when using the STL.

    The contents of each element will be the same, unless the copy constructor doesn’t work.

    int main(int argc, char *argv[])
    {
        int data[] = { 1, 2, 3 };
    
        std::vector vec(data, data + 3);
        // vector contains 1, 2, 3
    
        std::vector::iterator i = vec.begin();
        cout << *i << endl; // prints 1
        int &ref = *i;
        cout << ref << endl; // prints 1
    
        vec.resize(6, 99);
        // vector now contains 1, 2, 3, 99, 99, 99
    
        // WRONG! may crash, may do the wrong thing, might work...
        // cout << *i << endl;
    
        // WRONG! invalid reference
        // cout << ref << endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why doesn't this work? I have bypassed this before but i can't remember how
C# doesn't require you to specify a generic type parameter if the compiler can
There doesn't seem to be a dictionary.AddRange() method. Does anyone know a better way
Doesn't matter what I do, I simply can't get this to play a sound
It doesn't seem possible, but I was hoping someone may be aware of a
Why doesn't this work? Can Anyone tell me whats wrong with this onitemclicklistener? It
Why doesn't .NET regex treat \n as end of line character? Sample code: string[]
It doesn't appear that the Source property for the XAML WebView control is updating
Why doesn't Ctrl + C work to break a Python program that uses PyQt?
Does anyone know how can I replace this 2 symbol below from the string

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.