如何为不绑定模板参数的tr1 :: unordered_map定义宏/ typedef/etc?

Vin*_*wal 4 c++ xcode stl unordered-map hashmap

这可能是一个有点愚蠢的问题,但我只需要问一下.我试图在C++中使用unordered_map类,但不是每次都将它作为tr1 :: unordered_map引用,我想只使用关键字hashMap.我知道

typedef tr1::unordered_map<string, int> hashMap 
Run Code Online (Sandbox Code Playgroud)

但是这样可以修复键的数据类型和hashMap对应的值,而我希望有更多如下所示:

#define hashMap tr1::unordered_map
Run Code Online (Sandbox Code Playgroud)

我可以在哪里定义键的数据类型和值取决于要求,但这不起作用.以前有人遇到过这个问题吗?

谢谢

Phi*_*ipp 5

这是C++ 11之前C++中缺少的东西.在C++ 11中,您可以使用template using:

template<typename Key, typename Value>
using hashMap = tr1::unordered_map<Key, Value>;
Run Code Online (Sandbox Code Playgroud)

C++ 03的常用解决方法是使用type成员创建模板结构:

template<typename Key, typename Value>
struct hashMap {
  typedef tr1::unordered_map<Key, Value> type;
};
// then:
hashMap<string, int>::type myMap;
Run Code Online (Sandbox Code Playgroud)

从理论上讲,从类继承是可能的,但通常用户不会这样做,因为STL类不是要继承的.