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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:51:47+00:00 2026-06-15T11:51:47+00:00

From this answer : One place where you can run into a performance issue,

  • 0

From this answer:

One place where you can run into a performance issue, is not sizing the vector correctly to begin with.

So, how does one size a vector correctly when it is a property of a class? Is there a (best) way to set the capacity of vector (at initialisation)?

  • 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-15T11:51:48+00:00Added an answer on June 15, 2026 at 11:51 am

    Yes there is. See the reserve method. It will request that a vector’s capacity be at least enough to contain the number of elements sent as its argument. If you can anticipate an upper bound on the number of items that you want to store in a vector, then you can reserve that amount of space in your vector.

    Example from the above link –

    // vector::reserve
    #include <iostream>
    #include <vector>
    
    int main ()
    {
        std::vector<int>::size_type sz;
    
        std::vector<int> foo;
        sz = foo.capacity();
        std::cout << "making foo grow:\n";
        for (int i=0; i<100; ++i) {
            foo.push_back(i);
            if (sz!=foo.capacity()) {
                sz = foo.capacity();
                std::cout << "capacity changed: " << sz << '\n';
            }
        }
    
        std::vector<int> bar;
        sz = bar.capacity();
        bar.reserve(100);   // this is the only difference with foo above
        std::cout << "making bar grow:\n";
        for (int i=0; i<100; ++i) {
            bar.push_back(i);
    
            // This block will execute only once
            if (sz!=bar.capacity()) {
                sz = bar.capacity();
                std::cout << "capacity changed: " << sz << '\n';
            }
        }
    
        return 0;
    }
    

    You will see that as you add more elements to the foo vector, its capacity keeps increasing, but in the second case, since it has already reserved 100 element’s space, the capacity is changed only once.

    Here is a running example.

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

Sidebar

Related Questions

This is a CSS related question, I got one good answer from my previous
From this answer: When is a C++ terminate handler the Right Thing(TM)? It would
I'm having trouble figuring out how to implement the code from this answer. Android:
I am using the accepted answer from this question . The relevant code is
From this question & the answers - What is the correct answer for cout
This question arose from the discussion in the comments of this answer . First,
I just read this great answer from BalusC about how to upload files with
I am trying to integrated linkedIn using this question answer Posting LinkedIn message from
I've just read this snippet from another answer : When you create a block
I have been looking at the answer to this question: Pulling details from response

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.