这可能是一个有点愚蠢的问题,但我只需要问一下.我试图在C中使用unordered_map类,但不是每次都将它作为tr1 :: unordered_map引用,我想只使用关键字hashMap.我知道
typedef tr1::unordered_map<string,int> hashMap
但是这样可以修复键的数据类型和hashMap对应的值,而我希望有更多如下所示:
#define hashMap tr1::unordered_map
我可以在哪里定义键的数据类型和值取决于要求,但这不起作用.以前有人遇到过这个问题吗?
谢谢
解决方法
这是在C 11之前C中缺少的东西.在C 11中,您可以使用以下模板:
template<typename Key,typename Value> using hashMap = tr1::unordered_map<Key,Value>;
C 03的常用解决方法是使用类型成员创建模板结构:
template<typename Key,typename Value> struct hashMap { typedef tr1::unordered_map<Key,Value> type; }; // then: hashMap<string,int>::type myMap;
从理论上讲,从类继承是可能的,但通常用户不会这样做,因为STL类不是要继承的.