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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:29:07+00:00 2026-06-07T13:29:07+00:00

I have a vector<CustomClass*> and I put a lot of items in the vector

  • 0

I have a vector<CustomClass*> and I put a lot of items in the vector and I need fast access, so I don’t use list. How to set initial size of vector (for example to be 20 000 places, so to avoid copy when I insert new)?

  • 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-07T13:29:08+00:00Added an answer on June 7, 2026 at 1:29 pm
    std::vector<CustomClass *> whatever(20000);
    

    or:

    std::vector<CustomClass *> whatever;
    whatever.reserve(20000);
    

    The former sets the actual size of the array — i.e., makes it a vector of 20000 pointers. The latter leaves the vector empty, but reserves space for 20000 pointers, so you can insert (up to) that many without it having to reallocate.

    At least in my experience, it’s fairly unusual for either of these to make a huge difference in performance–but either can affect correctness under some circumstances. In particular, as long as no reallocation takes place, iterators into the vector are guaranteed to remain valid, and once you’ve set the size/reserved space, you’re guaranteed there won’t be any reallocations as long as you don’t increase the size beyond that.

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

Sidebar

Related Questions

I have a vector<list<customClass> > I have an iterator vector<list<customClass> >::const_iterator x When I
I have vector with 6 numbers, which I want insert to list and add
I have vector in vector in vector .... . But I don't know how
I have a vector with raw pointers (no, I cannot use smart pointers) and
I have Vector of elements that i set into BaseAdapter subclass and this adapter
I have a very large multidimensional vector that changes in size all the time.
When do we use synchronized ArrayList ? We already have Vector which is synchronized.
I have a vector that I want to insert into a set . This
If I have vector<T> list Where each element in the list is unique, what's
I have a vector which contains a list of files to open and parse,

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.