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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:27:26+00:00 2026-06-01T01:27:26+00:00

I have a vector for storing the pointers of all objects (created by new

  • 0

I have a vector for storing the pointers of all objects (created by new) of a class.
I have an int member variable (called id) for each object which stores the index of the vector element in which the address of that object is stored.
This method helps me to call any object with the help of id.
The id is assigned in the constructor with the help of a static variable, which is incremented each time an object is created.
My problem is that when i delete an object, i cannot delete the vector element (free the memory taken by that element, as my vector is created dynamically) (because if i delete it, then the indexes of all the next elements will decrease by 1).
So I would like to know a method by which i can free the memory from the vector but not cause change in indexes of other elements.
Please tell me a method which doesn’t consume a lot of time. (methods like rearranging elements & then changing id of each object will consume a lot of time!)
I may have around 1000 such objects which may consume a lot of time if i delete the 1st element & do rearrangement as mentioned above.
THANK YOU

  • 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-01T01:27:27+00:00Added an answer on June 1, 2026 at 1:27 am

    You might want to consider using a map<int,your_object_type*> instead of a vector<your_object_type*>. Then you won’t need to ‘reindex’ or (more importantly, I think) reassign IDs when an item is deleted.

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

Sidebar

Related Questions

I have a vector-like class that contains an array of objects of type T
Storing objects in heterogeneous vector with stack-allocated objects Hello, Say I have an abstract
I am storing dynamically allocated class pointers in a vector like below. vector<Test *>
I have a collection of polymorphic objects, all derived from my Animal class: Cat,
I have a class that has a vector of another class objects as a
Do class objects declared on the stack have the same lifetime as other stack
i have a specific problem with pointers and references, with std::vector and std::string. Some
I have this code to represent bank: class Bank { friend class InvestmentMethod; std::vector<BaseBankAccount*>
At the moment I am using a vector to store pointers to the object
I have some generic code for deleting pointers within a vector or a value

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.