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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:05:12+00:00 2026-06-15T16:05:12+00:00

I am working on a requirement where requests should have unique number from -2

  • 0

I am working on a requirement where requests should have unique number from -2 to -101 inclusive, i.e., there are unique 100 requests at a time. If there are more than 100 requests at a given time then I should send error. Initially I have no requests. Once I sent requests I will take unique number say -2 , -3 and so on. Here requirement is that I may get command from client don’t sent request to server for example -2 so I should delete this request and I should reuse this number for future request.

What is the best way to implement this in C++?

Also, I am not supposed to use Boost.

  • 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-15T16:05:13+00:00Added an answer on June 15, 2026 at 4:05 pm

    You have to maintain a collection of unused ids at least. Additionally I would throw in a lookup table to verify that an id was handed out (for robustness). For both, I would suggest to use an std::vector, not a list.

    First, store the unused collection in an std::vector<int>, which you can very easily initialise:

    class IdStore {
      private:
        std::vector<int> unused;
        static int const MIN_ID = -101;
        static int const MAX_ID = -2;
      public:
        IdStore::IdStore()
        : unused(MAX_ID - MIN_ID + 1) {
          for (auto i = 0; i <= MAX_ID-MIN_ID; ++i) {
            unused[i] = i;
          }
        }
        int getId();
        void releaseId(int);
    };
    

    Additionally, you may want to keep track of the used ids, so you can verify if they were handed out; I’d use an std::vector<bool> used; member for that, which you can initialise simply with used(MAX_ID - MIN_ID +1) as its values will all default to false initially. Of course, you can make used also a bitset but note that this would require the distance from MIN_ID to MAX_ID to be known at compile time.

    Handing out stuff is pretty simple from there:

    int IdStore::getId() {
      if (unused.empty())
        throw "error"; // put something better here
      auto r = unused.back();
      used[r] = true;
      unused.pop_back();
      return MIN_ID + r;
    }
    

    And releasing them, also:

    void IdStore::releaseId(int id) {
      if (id < MIN_ID || id > MAX_ID)
        throw "error"; // put something better here
      id -= MIN_ID;
      if (!used[id])
        throw "error"; // put something better here
      used[id] = false;
      unused.push_back(id);
    }
    

    Note that no reallocations take place! The vector will keep its size and neither getId nor releaseId will require expensive calls to malloc or free contrary to an approach using a list.

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

Sidebar

Related Questions

I am working in JTable and I have a requirement like this. Say There
I have a requirement for a program I'm working on to store job numbers
On the project that I'm currently working I have a requirement to run a
I am working on Android Application. Now I have requirement of search the entered
I'm working in Drupal 6. I have a requirement to add a particular block
at the project I am working on, we have a requirement to have a
I’m working on a grails web application with spring security. I have a requirement
I am working in Asp.net MVC and have a peculiar requirement for which I
I am working on asp.net mvc3 and i want a particular requirement that the
I'm working on a site that has a requirement to share session between a

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.