简体   繁体   English

有没有办法限制STL地图的大小?

[英]Is there any way to limit the size of an STL Map?

I want to implement some sort of lookup table in C++ that will act as a cache. 我想在C ++中实现某种用作缓存的查找表。 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). 我无意发明轮子所以我打算使用std::map (虽然欢迎提供替代方案的建议)。

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. 我希望哈希的insert方法返回错误消息,或者如果达到限制则抛出异常。

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. 如果没有这种方式,我会在尝试插入之前检查它的大小,但这似乎是一种不太优雅的方式。

First thing is that a map structure is not a hash table , but rather a balanced binary tree. 首先, map结构不是hash table ,而是平衡的二叉树。 This has an impact in the lookup times O(log N) instead of O(1) for an optimal hash implementation. 对于最佳散列实现,这对查找时间O(log N)而不是O(1)有影响。 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. 您可以做的最简单的解决方案是将实际数据结构封装到一个类中,该类检查每次插入的大小(大小查找应该在所有STL实现中以恒定时间实现),如果您尝试添加太多元素,则会失败。

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;

There is no way to "automatically" limit the size of an std::map , simply because a "limited std::map " would not be an std::map anymore. 没有办法“自动”限制std::map的大小,只是因为“有限的std::map ”不再是std::map

Wrap the std::map in a class of your own which just checks the size of the std::map against a constant before inserting, and does whatever you want in case the maximum size has been reached. std::map包装在您自己的类中,它只是在插入之前检查std::map的大小与常量,并在达到最大大小时执行任何操作。

If you're implementing LRU caches or similar, I've found boost::bimap to be invaluable. 如果你正在实现LRU缓存或类似的,我发现boost :: bimap非常宝贵。 Use one index to be the primary "object ID" key (which can be an efficient vector view if you have a simple enumeration of objects), and the other index can be an ordered timestamp. 使用一个索引作为主“对象ID”键(如果您有简单的对象枚举,则可以是高效的矢量视图),另一个索引可以是有序时间戳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM