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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:19:34+00:00 2026-06-12T10:19:34+00:00

According to Stroustrup : The C++ programming language :- When a vector is resized

  • 0

According to Stroustrup : The C++ programming language :-

“When a vector is resized to accommodate more (or fewer) elements, all of its elements may be
moved to new locations.”

Is this holds true, even if the vector is re-sized to smaller size ?

  • 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-12T10:19:36+00:00Added an answer on June 12, 2026 at 10:19 am

    Case 1: If the new size being requested is greater than the current std::vector::capacity() then all elements will be relocated.
    Case 2: If the new size being requested is lesser than the current std::vector::capacity() then there will be no relocation of elements.


    Standerdese Evidence:

    The standard defines effect of vector::resize() as:

    C++11 Standard 23.3.6.3/12 vector capacity:

    void resize(size_type sz, const T& c);
    

    Effect:

    if (sz > size())
         insert(end(), sz-size(), c);
    else if (sz < size())
        erase(begin()+sz, end());
    else
    ; // do nothing
    

    As @DavidRodríguez-dribeas correctly points out, Iterator invalidation rules for std::vector::insert() operation are:

    23.3.6.5 vector modifiers

    1 [insert,push_back,emplace,emplace_back]

    Remarks: Causes reallocation if the new size is greater than the old capacity. If no reallocation happens, all the iterators and references before the insertion point remain valid.

    Essentially this means:
    All iterators and references before the point of insertion will be unaffected, unless the new container size is greater than the previous capacity because in such a scenario all elements might be moved to new locations thus invalidating pointers/iterators to original location.Since resize() only erases/inserts elements at the end of the container[Note 1].The governing factor boils down to size being requested as against current capacity.

    Hence the Case 1 result.

    In Case 2 std::vector::erase() will be called and the invalidation rule in this case is:

    23.3.6.5 vector modifiers

    iterator erase(const_iterator position);

    3 Effects: Invalidates iterators and references at or after the point of the erase.

    Since [Note 1], elements will be only removed at end and there is no need of relocation of all elements.

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

Sidebar

Related Questions

In Stroustrup's C++ Programming Language book (3rd edition), in the Numerics chapter he shows
According to Bjarne Stroustrup: if (and only if) you use an initialized member in
According to the PHP Documentation PDO::prepare() adds quotes to all your parameters so that
According to Bjarne Stroustrup, references were introduced into C++ to support operator overloading: References
According Mac App Store rules 2.30: Program may write only to allowed locations. My
According to API Dock, ActiveRecord::Associations::AssociationProxy is now deprecated or moved. I'm curious as to
According to Wikipedia, on the Comparison of programming languages page, it says that F#
According documentation: System.Array.Sort<T> - sorts the elements in an entire System.Array using the System.IComparable
According to C++ Standard paragraph 3.7.3/1 objects should be dynamically created with new expression
according to the CUDA programming guide, the value returned by the texture fetch is

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.