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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:17:53+00:00 2026-05-27T19:17:53+00:00

I have a map that stores pointers to an object by their ID. typedef

  • 0

I have a map that stores pointers to an object by their ID.

typedef std::map<unsigned int, Entity*> entityMap;
entityMap entitymap;

To assign an ID to Entity I could just take the newest value in entitymap and increase it by 1.

Entity *entity = new Entity;
entity->id = /*newest entity+1*/;
entitymap.insert(std::pair<unsigned int,Entity*>(entity->id,entity));

But the number could become unnecessarily big because every now and then an Entity is deleted and removed from the map.

std::map<unsigned int,Entity*>::iterator it;
it = entitymap.find(EntityID);
if(it != entitymap.end())
{
    Entity *entity= it->second;
    entitymap.erase(it);
}
delete entity;

So I could have a map that holds these values;

1,2,4,8,10

In which case I’d like the next Entity to claim the ID 3.

  • 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-27T19:17:54+00:00Added an answer on May 27, 2026 at 7:17 pm

    Since the IDs are ordered numerically, you could walk through the entire map until you find a “hole”:

    unsigned int i = 1; // or whatever your smallest admissable key value is
    
    for (auto it = m.cbegin(), end = m.cend();
                               it != end && i == it->first; ++it, ++i)
    { }
    
    // now i is the next free index
    

    This may take long if the map is large and the first hole is near the end. You could check first if the largest key value (given by m.crbegin()->first) is significantly larger than m.size() before embarking on this exploration.

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

Sidebar

Related Questions

I have a template class that contains a std::map that stores pointers to T
I have a std::map that I'm using to store values for x and y
I have a map that represents a DB object. I want to get 'well
I have a Map, that I want to persist. The domain object is something
I have a std::map that is used by multiple threads to store data. The
Suppose I have an entity class that stores a username: class Profile { private
I have a Map that uses a Set for the key type, like this:
I want to have a map that has a homogeneous key type but heterogeneous
I have a Google Map that suddenly stopped working for no apparent reason (I
In a controller, I have populated a map that has a string as key

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.