C ++ typedef a std :: pair and then use typedef to declare a map

Say I have this typedef

typedef std::pair<std::string, uint32_t> MyType;

Then, if I also want to create a map using MyType, how can I do this?

I don't want to re-enter two types in pairs, for example:

map<std::string, uint32_t> myMap;

I need something like:

map<MyType first type, MyType second type> myMap;

Is there a way to do this so using my typedef MyType instead of retyping the types?

+4
source share
3 answers

Simply...

std::map<MyType::first_type, MyType::second_type> myMap;

See http://en.cppreference.com/w/cpp/utility/pair

Program example in coliru

+5
source

If you intend to do this with many different types, you can customize the declaration of an alias.

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;

This requires at least C ++ 11.

+3

, . . , , . . typedef . , "std::", std; .

0

Source: https://habr.com/ru/post/1624731/


All Articles