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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:59:14+00:00 2026-05-14T08:59:14+00:00

I want to implement some sort of lookup table in C++ that will act

  • 0

I want to implement some sort of lookup table in C++ that will act as a cache. It is meant to emulate a piece of hardware I’m simulating.

The keys are non-integer, so I’m guessing a hash is in order. I have no intention of inventing the wheel so I intend to use std::map for this (though suggestions for alternatives are welcome).

The question is, is there any way to limit the size of the hash to emulate the fact that my hardware is of finite size? I’d expect the hash’s insert method to return an error message or throw an exception if the limit is reached.

If there is no such way, I’ll simply check its size before trying to insert, but that seems like an inelegant way to do it.

  • 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-14T08:59:15+00:00Added an answer on May 14, 2026 at 8:59 am

    First thing is that a map structure is not a hash table, but rather a balanced binary tree. This has an impact in the lookup times O(log N) instead of O(1) for an optimal hash implementation. This may or not affect your problem (if the number of actual keys and operations is small it can suffice, but the emulation of the cache can be somehow suboptimal.

    The simplest solution that you can do is encapsulate the actual data structure into a class that checks the size in each insertion (size lookups should be implemented in constant time in all STL implementations) and fails if you are trying to add one too many elements.

    class cache {
    public:
       static const int max_size = 100;
       typedef std::map<key_t, value_t> cache_map_t;
       void add( key_t key, value_t value ) {
          cache_map_t::const_iterator it = m_table.find( key );
          if ( it != m_table.end() && m_table.size() == max_size ) { 
             // handle error, throw exception...
          } else {
             m_table.insert( it, std::make_pair(key,value) );
          }
       }
    private:
       cache_map_t m_table;
    };
    // Don't forget, in just one translation unit:
    const int cache::max_size;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a little project and want to implement some sort of
I want to implement some basic tracing in a simple c# (WPF) application that
We want to implement a sitemap.xml feature in out CMS system. There is some
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement a REST-api in C#. I found that WCF Webapi can
I'm trying to implement some sort of 'just-for-me' game engine and the problem's plot
I'm working on a dummy Unix-like OS (OS161) and I will want to implement
I'm trying to implement some STL-style sorting algorithms. The prototype for std::sort looks something
I'm trying to write a Python class that acts like some sort of datastore.
I need to implement some sort of data locking in a Flex application I'm

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.