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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:37:07+00:00 2026-05-26T01:37:07+00:00

before push back, i shrink capacity to zero, then i push back item into

  • 0
  1. before push back, i shrink capacity to zero, then i push back item into vector, will it reallocate?
  2. is reallocate means slow down in performance?
  3. if so, why need to shrink capacity to zero or fit the size?

    vector<int> mandy2(5);
    vector<int>().swap(mandy2);
    
    printf("mandy2 size: %d\n", mandy2.size());
    printf("mandy2 capacity: %d\n", mandy2.capacity());
    
    mandy2.push_back(1);
    mandy2.push_back(2);
    mandy2.push_back(6);
    
  • 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-26T01:37:07+00:00Added an answer on May 26, 2026 at 1:37 am

    before push back, i shrink capacity to zero, then i push back item into vector, will it reallocate?

    Yes it will.

    Reference the doccumentation:

    void push_back ( const T& x );

    Adds a new element at the end of the vector, after its current last element. The content of this new element is initialized to a copy of x.

    This effectively increases the vector size by one, which causes a reallocation of the internal allocated storage if the vector size was equal to the vector capacity before the call. Reallocations invalidate all previously obtained iterators, references and pointers.


    Is reallocate means slow down in performance?

    Theorotically,Yes!
    How much it hurts you can only be conclusively said after profiling your code for the same.


    If so, why need to shrink capacity to zero or fit the size?

    You do that only, if you know that you do not want to add any further elements to the vector, that ensures your vector does not consume more memory than you would actually need ever.

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

Sidebar

Related Questions

Before C++11 I have used swap-to-back to avoid deep copy overheads, like: vector<vector<Object> >
Is it possible to automatically encrypt files via 'git push' before transferring to a
Before I jump headlong into C#... I've always felt that C, or maybe C++,
I am not sure what's wrong with this code : std::vector<int> myVector(0); if (myVector.back()
I have a large tests suite, that needs to be run before I push
I want to push_back() a vector M into vector N . #include <iostream> #include
What happens if I clear a vector which was already cleared before? I tried
NOTE : This question was originally asked way back in 2012. Before the decltype
Before you answer this I have never developed anything popular enough to attain high
Before I start, I know there is this post and it doesn't answer my

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.