c++ - Iterate using an enum -


i have std::map key enum this:

enum class my_enum{a=1,b,c,d}; std::map<my_enum,my_class> my_map; 

i want iterate on map used for:

for (auto current_type = (id_type)0;      (int)current_type < 4;      current_type = (my_enum)((int)current_type + 1)){    //do things } 

i think rubbish. have better suggestion?

edit:

i totally aware of using of iterators. know prefereable. however, reason, have stick regular index-based for.

define types this:

enum class my_enum{a=1, b, c, d, end}; std::map<my_enum,my_class> my_map;  my_enum& operator++( my_enum &val ) {   using inttype = typename std::underlying_type<my_enum>::type   val = static_cast<my_enum>( static_cast<inttype>(val) + 1 );   return val; } 

and can use them this:

for (my_enum e = my_enum::a; e < my_enum::end; ++e) {    my_class c = my_map[e];    .... } 

(although, might prefer use other operator[] if don't want create null-values entries don't exist.)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -