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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:28:08+00:00 2026-05-25T20:28:08+00:00

I am experiencing a problem with the vector container. I am trying to improve

  • 0

I am experiencing a problem with the vector container. I am trying to improve the performance of inserting a lot of elements into one vector.

Basically I am using vector::reserve to expand my vector _children if needed:

if (_children.capacity() == _children.size())
{
     _children.reserve(_children.size() * 2);
}

and using vector::at() to insert a new element at the end of _children instead of vector::push_back():

_children.at(_children.size()) = child;

_children has already one element in it, so the first element should be inserted at position 1, and the capacity at this time is 2.

Despite this, an out_of_range error is thrown. Can someone explain to me, what I misunderstood here? Is it not possible to just insert an extra element even though the chosen position is less than the vector capacity? I can post some more code if needed.

Thanks in advance.

/mads

  • 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-25T20:28:09+00:00Added an answer on May 25, 2026 at 8:28 pm

    Increasing the capacity doesn’t increase the number of elements in the vector. It simply ensures that the vector has capacity to grow up to the required size without having to reallocate memory. I.e., you still need to call push_back().

    Mind you, calling reserve() to increase capacity geometrically is a waste of effort. std::vector already does this.

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

Sidebar

Related Questions

We are experiencing a performance problem when communicating with the active directory using System.DirectoryServices.
I'm experiencing a strange problem when trying to count the entities in a managed
i'm currently experiencing a little problem. I'm using nhibernate with around 40 entities mapped
I'm trying to style my form buttons and I'm experiencing a problem in Firefox
using yslow on a project I am experiencing a problem I can not solve.
I'm experiencing a problem with MySQL's GROUP_CONCAT function. I will illustrate my problem using
Appparantly I'm the only one on the Internet experiencing this problem because I can
I'm trying to use ProGuard to obfuscate a Java application. I'm experiencing a problem
I'm experiencing a problem while trying to use google maps api v3 (with marker
i am experiencing a problem with using the delay in jQuery's .animation function, when

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.