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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:46:40+00:00 2026-05-17T17:46:40+00:00

In my data structures course, we are creating a basic vector class. In my

  • 0

In my data structures course, we are creating a basic vector class. In my header, there is a member function for inserting new values into the vector. If the capacity of my vector is big enough for the insertion of one value, the program runs fine. However, if the vector needs to grow, I run into pointer errors. After hours of trying to figure out what was happening, I believe that I’m running into a problem with invalidating iterators.

Here are my insert function, and reserve function:

// Insert "item" before "pos", and return iterator referencing "item"

iterator insert (iterator pos, const T& item)
{

if (m_size ==  m_capacity)
  {
  reserve(m_capacity * 2);
  }

T * endOfArray = (end());

while (endOfArray != pos)
  {
  *(endOfArray) = *(endOfArray - 1);
  --endOfArray;
  }
++m_size;
*pos = item;

return (pos);
}

void reserve (size_t space) {
if (space < m_capacity)
  {
  return;
  }

T *temp_array = new T[space]; 

std::copy(begin(), end(), temp_array);
m_array = new T[space];
std::copy(temp_array, (temp_array + m_size), m_array);
m_capacity = space;
delete [] temp_array;
} 

As you can see, if the insert function calls to have the capacity of the vector increased, then the “pos” iterator invalidates. How can I get around this problem?

  • 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-17T17:46:41+00:00Added an answer on May 17, 2026 at 5:46 pm

    Change the pos iterator into an index instead, and use that.

    size_t index = pos - begin();
    // do resize
    pos = begin() + index;
    

    If you’re worried about iterators invalidating in general, don’t. It’s unavoidable, at least in this style of dynamic array, and std::vector works exactly the same.

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

Sidebar

Related Questions

I took a data structures class in C++ last year, and consequently implemented all
One of the basic data structures in Python is the dictionary, which allows one
Let's say I have data structures that're something like this: Public Class AttendenceRecord Public
Are there any built-in C# data structures that are like a hash table but
.NET has a lot of complex data structures. Unfortunately, some of them are quite
I have the following problem in my Data Structures and Problem Solving using Java
I want to be good in Data Structures and Analysis esp. in Java. I
What is the fastest way to share data structures between Java and C#? I
I'm looking for material on persistent data structures that can be used to implement
Should the model just be data structures? Where do the services (data access, business

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.