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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:13:35+00:00 2026-06-04T20:13:35+00:00

By reading the std::vector reference I understood that calling insert when the the maximum

  • 0

By reading the std::vector reference I understood that

  1. calling insert when the the maximum capacity is reached will cause the reallocation of the std::vector (causing iterator invalidation) because new memory is allocated for it with a bigger capacity. The goal is to keep the guarantee about contiguous data.

  2. As long as I stick below the maximum capacity insert will not cause that (and iterators will be intact).

My question is the following:

When reserve is called automatically by insert, is there any way to control how much new memory must be reserved?

Suppose that I have a vector with an initial capacity of 100 and, when the maximum capacity is hit, I want to allocate an extra 20 bytes.

Is it possible to do that?

  • 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-04T20:13:37+00:00Added an answer on June 4, 2026 at 8:13 pm

    You can always track it yourself and call reserve before it would allocate, e.g.

    static const int N = 20 // Amount to grow by
    if (vec.capacity() == vec.size()) {
      vec.reserve(vec.size() + N);
    }
    vec.insert(...);
    

    You can wrap this in a function of your own and call that function instead of calling insert() directly.

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

Sidebar

Related Questions

after reading inputs to an array: std::vector<unsigned char *> inputs(elements); the inputs is a
I'm having some troubles with using the std::sort algorithm here. I was reading that
I came across this question, while reading about std::array and std::vector .
I want to wrap a vector<char> with std::istream (so reading the vector would be
I'm trying to store strings that I read from a file in a std::vector
After reading about the problem of passing empty std::string objects between DLLs and EXEs,
I am reading the IEEE Standard Verilog Hardware Description Language (specifically IEEE Std 1364-2001)
Reading the documentation for the spawn gem it states: By default, spawn will use
Reading the code of many javascript libraries, I see that many developers use to
During program optimization, trying to optimize a loop that iterates through a vector, I

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.