whi*_*kar 5 c++ dictionary typedef std-pair
假设我有这种typedef
typedef std::pair<std::string, uint32_t> MyType;
Run Code Online (Sandbox Code Playgroud)
然后,如果我还想使用MyType创建地图,我该怎么做?
我不想重新键入对中的两种类型:
map<std::string, uint32_t> myMap;
Run Code Online (Sandbox Code Playgroud)
我想要的东西:
map<MyType's first type, MyType's second type> myMap;
Run Code Online (Sandbox Code Playgroud)
有没有办法像使用我的typedef MyType而不是重新键入类型?
只是...
std::map<MyType::first_type, MyType::second_type> myMap;
Run Code Online (Sandbox Code Playgroud)
见http://en.cppreference.com/w/cpp/utility/pair
示例程序在 coliru
如果您要对许多不同的类型执行此操作,则可以设置别名声明。
template <typename T>
using pair_map = std::map< typename T::first_type, typename T::second_type>;
typedef std::pair<std::string, int> MyType;
pair_map<MyType> my_map;
Run Code Online (Sandbox Code Playgroud)
这至少需要 c++11。