Jump to content
  • Advertisement
Sign in to follow this  
riruilo

question about std::vector and reserve or capacity

This topic is 4120 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! Just one question. I´m storing my objects pointers in a std::vector. I use push_back to add my pointers. My question is: Should I use reserve or capacity methods? or it is not neccesary. I mean, in a efficient point of view. By the way, I know the number of my elements and they are fixed. Thanks you very much!

Share this post


Link to post
Share on other sites
Advertisement
Every time your vector gets larger than the memory it has allocated it need to allocate new memory and copy over the objects. So if you know how many objects you are going to store, use reserve. It will most probably be more efficient.

Share this post


Link to post
Share on other sites
If you know how many elements you will be storing you can use reserve and then push_back them into the vector. It's a good habit to get into. You shouldn't need to use capacity during normal use.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!