【发布时间】:2016-11-02 08:44:53
【问题描述】:
根据我之前的问题Defining a class member according to the class template,我意识到unordered_map 的默认存储桶计数对于我的目的来说太低了。
我有一个类模板Base,它将使用映射或无序映射,具体取决于模板参数:
template<class A, class B>
class Base {
template<class T1, class T2>
struct MapType { typedef boost:unordered_map<...> MType; };
template<class T2>
struct MapType<std::string, T2> { typedef std::map<...> MType; };
typedef typename MapType<...>::MType Map;
Map mMap;
};
我想通过使用其构造函数(第一个参数定义大小)或使用无序映射rehash 函数来更改Map 的默认大小。
到目前为止,我唯一的想法是使用 Base 类构造函数来检查 (dynamic_cast?) 我的 mMap 是映射还是无序映射,然后使用 rehash 函数。
唯一的限制是这段代码被用于数百个不能改变的地方(不能对我的基类进行多态化)。
【问题讨论】: